.Net datetime with milliseconds sql server using bulkcopy throws exception
.Net datetime with milliseconds sql server using bulkcopy thows exception
我有这段代码,它抛出异常。
SqlConnection con;
con = new SqlConnection(connectionStr);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @"select [ID], [VERSION_ID], '08/26/2015 09:33:24:717 AM' as [Added_Dt], [Loc_ID] as Column1
from dbo.Source where Added_Dt between CONVERT(DATETIME,'08/24/2015 09:25:43:283 AM') and CONVERT(DATETIME,'08/24/2015 09:25:43:283 AM')";
cmd.CommandTimeout = con.ConnectionTimeout;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
System.Data.SqlClient.SqlBulkCopy bcp = new SqlBulkCopy(destconnectionStr, SqlBulkCopyOptions.UseInternalTransaction);
bcp.BatchSize = (int)Global.BatchWriteThreshold;
bcp.DestinationTableName = destinationTableName;
bcp.NotifyAfter = (int)Global.BatchWriteThreshold;
bcp.SqlRowsCopied += new SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.WriteToServer(rdr);
destination table(SQL Server 2005): ID (int, not null), VERSION_ID (int, not
null),Added_Dt(DATETIME,not null),Column1(varchar2(50) ,not null)
如果与 SqlBulkCopy 一起使用会引发异常,但 运行 在单个更新中或直接从 SQL Management Studio 使用 ado.net 时没有任何问题。我发现删除毫秒部分使 bulkcopy 运行 无一例外,但我希望毫秒在那里。如何解决问题?
'08/24/2015 09:25:43:283 AM'
不是有效的日期字符串。最后的 :
应该是 .
。含义:
'08/24/2015 09:25:43.283 AM'
当然,这在 SSMS 中确实可以正确转换:
SELECT CONVERT(DATETIME,'08/24/2015 09:25:43:283 AM');
如果修复无效,那么您需要提供确切的异常消息。
我有这段代码,它抛出异常。
SqlConnection con;
con = new SqlConnection(connectionStr);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @"select [ID], [VERSION_ID], '08/26/2015 09:33:24:717 AM' as [Added_Dt], [Loc_ID] as Column1
from dbo.Source where Added_Dt between CONVERT(DATETIME,'08/24/2015 09:25:43:283 AM') and CONVERT(DATETIME,'08/24/2015 09:25:43:283 AM')";
cmd.CommandTimeout = con.ConnectionTimeout;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
System.Data.SqlClient.SqlBulkCopy bcp = new SqlBulkCopy(destconnectionStr, SqlBulkCopyOptions.UseInternalTransaction);
bcp.BatchSize = (int)Global.BatchWriteThreshold;
bcp.DestinationTableName = destinationTableName;
bcp.NotifyAfter = (int)Global.BatchWriteThreshold;
bcp.SqlRowsCopied += new SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.WriteToServer(rdr);
destination table(SQL Server 2005): ID (int, not null), VERSION_ID (int, not null),Added_Dt(DATETIME,not null),Column1(varchar2(50) ,not null)
如果与 SqlBulkCopy 一起使用会引发异常,但 运行 在单个更新中或直接从 SQL Management Studio 使用 ado.net 时没有任何问题。我发现删除毫秒部分使 bulkcopy 运行 无一例外,但我希望毫秒在那里。如何解决问题?
'08/24/2015 09:25:43:283 AM'
不是有效的日期字符串。最后的 :
应该是 .
。含义:
'08/24/2015 09:25:43.283 AM'
当然,这在 SSMS 中确实可以正确转换:
SELECT CONVERT(DATETIME,'08/24/2015 09:25:43:283 AM');
如果修复无效,那么您需要提供确切的异常消息。