I am doing a bulk insert:
// Get the data into the DataTable
//dtData = GetData(...);
// Create an object of SqlBulkCopy
SqlBulkCopy objSBC = new SqlBulkCopy(connection);
// Specify the destination table
objSBC.BulkCopyTimeout = 0;
objSBC.BatchSize = 10000;
objSBC.DestinationTableName = "QuickLabDump";
// Write the data to the SQL Server
objSBC.WriteToServer(QuickLabDump);
my datatable that i am inserting looks like this:
QuickLabDump = new DataTable();
QuickLabDump.Columns.Add("Time Collected", typeof(TimeSpan));
QuickLabDump.Columns.Add("Time Entered", typeof(TimeSpan));
QuickLabDump.Columns.Add("Time Completed", typeof(TimeSpan));
QuickLabDump.Columns.Add("Test Time", typeof(TimeSpan));
QuickLabDump.Columns.Add("Date Collected", typeof(DateTime));
QuickLabDump.Columns.Add("Date Entered", typeof(DateTime));
QuickLabDump.Columns.Add("Date Completed", typeof(DateTime));
QuickLabDump.Columns.Add("Test Date", typeof(DateTime));
......
When I run the bulk insert from c# (code above), I am getting the following error:
The given value of type DateTime from the data source cannot be converted to type time of the specified target column.
I think the problem is that I am inserting typeof(DateTime) into a sql server 2008 table that is time(0)
Here’s a sample of the data that I am inserting:
6:50:00 AM
6:50:00 AM
6:50:00 AM
10:36:00 AM
4:45:00 PM
7:39:00 PM
Question: how do I define a datatable column properly so that it will insert the above time values into a time(0) column?
Here are values that already exist in the database table in the time(0) fields:
14:57:00
14:58:00
14:58:00
14:57:00
10:49:00
13:31:00
14:02:00
14:13:00
14:20:00
14:56:00
15:00:00
This is how I am adding data into DataTable:
public DataTable dt;
public ReadFileIntoDataTable(string inputfile)
{
using (GenericParserAdapter parser = new GenericParserAdapter())
{
parser.SetDataSource(inputfile);
char[] delimiters = new char[] { ',' };
parser.ColumnDelimiter = delimiters[0];
parser.FirstRowHasHeader = true;
//parser.SkipDataRows = 10;
parser.MaxBufferSize = 4096;
//parser.MaxRows = 500;
parser.TextQualifier = '\"';
dt = parser.GetDataTable();
}
i created explicit column mappings between my datatable and the sql server table:
for all columns…..
and that did the trick!!!