Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Received an invalid column length from the bcp client for colid 6

I want to bulk upload csv file data to sql server 2005 from c# code but I am encountering the below error -

Received an invalid column length from the bcp client for colid 6.

when bulk copy write to database server

like image 598
Sana Sana Avatar asked May 04 '12 04:05

Sana Sana


1 Answers

I know this post is old but I ran into this same issue and finally figured out a solution to determine which column was causing the problem and report it back as needed. I determined that colid returned in the SqlException is not zero based so you need to subtract 1 from it to get the value. After that it is used as the index of the _sortedColumnMappings ArrayList of the SqlBulkCopy instance not the index of the column mappings that were added to the SqlBulkCopy instance. One thing to note is that SqlBulkCopy will stop on the first error received so this may not be the only issue but at least helps to figure it out.

try {     bulkCopy.WriteToServer(importTable);     sqlTran.Commit(); }     catch (SqlException ex) {     if (ex.Message.Contains("Received an invalid column length from the bcp client for colid"))     {         string pattern = @"\d+";         Match match = Regex.Match(ex.Message.ToString(), pattern);         var index = Convert.ToInt32(match.Value) -1;          FieldInfo fi = typeof(SqlBulkCopy).GetField("_sortedColumnMappings", BindingFlags.NonPublic | BindingFlags.Instance);         var sortedColumns = fi.GetValue(bulkCopy);         var items = (Object[])sortedColumns.GetType().GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sortedColumns);          FieldInfo itemdata = items[index].GetType().GetField("_metadata", BindingFlags.NonPublic | BindingFlags.Instance);         var metadata = itemdata.GetValue(items[index]);          var column = metadata.GetType().GetField("column", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).GetValue(metadata);         var length = metadata.GetType().GetField("length", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).GetValue(metadata);         throw new DataFormatException(String.Format("Column: {0} contains data with a length greater than: {1}", column, length));     }      throw; }
like image 198
b_stil Avatar answered Oct 06 '22 20:10

b_stil