Can't insert data table using sqlbulkcopy

筅森魡賤 提交于 2019-12-13 11:35:44

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/9273243/cant-insert-data-table-using-sqlbulkcopy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!