简体   繁体   English

无法使用sqlbulkcopy插入数据表

[英]Can't insert data table using sqlbulkcopy

This is my code with the following columns and in the DB, those columns are nvarchars . 这是我的代码,包含以下列,在DB中,这些列是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. 数据源中String类型的给定值无法转换为指定目标列的类型nvarchar。

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. 如果您的ShortText列定义为NVARCHAR(30)并且您有一个40个字符的字符串,则可能会出现该错误。 See here for example: http://forums.asp.net/p/1228890/2212376.aspx 请参见此处: http//forums.asp.net/p/1228890/2212376.aspx

I have another answer for this question, maybe it can come in handy for someone. 我对这个问题有另一个答案,也许它可以派上用场。 If you have a String which contains " (quotation mark), you get this error message too. At least I just got this error message, and I searched for the wrong row, and I found this. As I removed the quotation marks, the error disappeared. 如果你有一个包含" (引号)的字符串,你也会得到这个错误信息。至少我得到了这个错误信息,我搜索了错误的行,我发现了这个。当我删除了引号时,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM