I am trying to do bulk insert throught a migration in C#, so far looks like this
public override void Up()
{
Sql("BULK INSERT Test FROM 'C:\\test.csv'
WITH (
FORMATFILE = 'C:\\format.xml',
FIRSTROW = 2);"
);
}
test.csv
Id Name Birthday Status
1 John 23-11-1984 00:00 OK
2 Peter 17-05-1986 00:00 N/A
3 James 18-03-1987 00:00 OK
format.xml
<?xml version="1.0"?>
<BCPFORMAT
xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="12"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="20" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="30"/>
<FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="Id" xsi:type="SQLINT"/>
<COLUMN SOURCE="2" NAME="Name" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="3" NAME="Birthday" xsi:type="SQLDATETIM8"/>
<COLUMN SOURCE="4" NAME="Status" xsi:type="SQLBIT"/>
</ROW>
</BCPFORMAT>
Test Table definition
Id (PK, not null)
Name (nvarchar(max), null)
Birthday (datetime, null)
Status (bit, null)
After updatig database I encountered first error (date format for Birthday in CSV is DD-MM-YYYY HH:MM):
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 3 (Birthday).
But if I change date format to YYYY-MM-DD HH:SS in the CSV file, it works.
Second issue is that for field 'Active' in CSV, I need to take the strings "OK" or "N/A" and in some way convert it and insert it in the table as Bit.
So at this point, my question is:
- Is there any way to convert this data before insert it into the table?