.Net 使用SqlBulkCopy提示“从 bcp 客户端收到一个对 colid 23 无效的列长度”问题处理

在使用SqlBulkCopy,进行批量数据插入时,有时会提示“从 bcp 客户端收到一个对 colid 23 无效的列长度”,其实看这个,我们都知道是数据库字段长度不够导致的,那问题来了,是哪个字段长度不够呢?其实,可以使用SqlException来做处理,代码如下:

using (SqlConnection conn = new SqlConnection(connStr))
                {
                    if (conn.State == ConnectionState.Closed) { conn.Open(); }

                    SqlTransaction tran = conn.BeginTransaction();
                    SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran);
                    try
                    {
                        foreach (var dic in dicTable)
                        {
                            if (dic.Value != null && dic.Value.Rows.Count != 0)
                            {
                                bulkCopy.DestinationTableName = dic.Key;
                                bulkCopy.BatchSize = dic.Value.Rows.Count;
                                bulkCopy.ColumnMappings.Clear();
                                foreach (DataColumn dtColumn in dic.Value.Columns)
                                {
                                    bulkCopy.ColumnMappings.Add(dtColumn.ColumnName, dtColumn.ColumnName);
                                }
                                bulkCopy.WriteToServer(dic.Value);
                            }
                        }
                        tran.Commit();
                    }
                    catch (SqlException ex)
                    {
                        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 Exception(String.Format("字段: {0} 当前长度: {1},小于数据长度,请扩展其长度!", column, length));
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw ex;
                    }
                    finally
                    {
                        if (bulkCopy != null)
                            bulkCopy.Close();
                        conn.Close();
                    }
                }

你可能感兴趣的:(.NetCore,SqlServer,C#,.net,SqlBulkCopy)