我正在做一个批量插入:
// 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);
我插入的数据表如下所示:
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));
......
当我从 c#(上面的代码)运行批量插入时,我收到以下错误:
The given value of type DateTime from the data source cannot be converted to type time of the specified target column.
我认为问题在于我正在插入typeof(DateTime)
一个 sql server 2008 表time(0)
这是我插入的数据示例:
6:50:00 AM
6:50:00 AM
6:50:00 AM
10:36:00 AM
4:45:00 PM
7:39:00 PM
问题:如何正确定义数据表列,以便将上述时间值插入time(0)
列中?
以下是数据库表中 time(0) 字段中已存在的值:
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
这就是我将数据添加到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();
}