且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用SqlBulkCopy的无法插入数据表

更新时间:2023-01-29 20:38:49

它的可能的被一个太短柱造成的。如果您ShortText列被定义为NVARCHAR(30),你有一个40字符的字符串,你可能会得到这个错误。看到这里,例如: http://forums.asp.net/p/1228890/2212376.aspx


This is my code with the following columns and in the DB, those columns are nvarchars.

SqlBulkCopy bulkCopy = new SqlBulkCopy(connection, 
System.Data.SqlClient.SqlBulkCopyOptions.Default, transaction);

bulkCopy.DestinationTableName = "Test";
bulkCopy.ColumnMappings.Add("Number", "Code");
bulkCopy.ColumnMappings.Add("Type", "Type");
bulkCopy.ColumnMappings.Add("Group", "Group");
bulkCopy.ColumnMappings.Add("Short Text", "ShortText");
bulkCopy.ColumnMappings.Add("Text", "Description");
bulkCopy.WriteToServer(dataTable);

I am trying to insert a whole data table in a DB, using bulk copy but I am getting this error:

The given value of type String from the data source cannot be converted to type nvarchar of the specified target column.

It may be caused by a too-short-column. If your ShortText column is defined as NVARCHAR(30) and you have a 40 character string, you may get that error. See here for example: http://forums.asp.net/p/1228890/2212376.aspx