Nib*_*Pig 6 c# sql-server datatable sqlbulkcopy
它适用于所有其他数据类型,但我无法使用'bit'列.
这是我用于批量写入的SQL:
using (var bulk = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity & SqlBulkCopyOptions.KeepNulls))
{
bulk.BatchSize = 2000;
bulk.DestinationTableName = targetTable;
bulk.WriteToServer(dataTable);
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据表:
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("MyBool", typeof(bool)); // Tried with and without typeof(bool)
return dt;
Run Code Online (Sandbox Code Playgroud)
这是我在将行添加到数据表之前构造行的方法.
personAssociationRow["MyBool"] = true;
WriteToServer行会抛出异常,具体取决于是否typeof(bool)指定了:
Cannot insert the value NULL into column 'MyBool', table 但是intellisense/debugger将值显示为 true
要么
The given value of type String from the data source cannot be converted to type int of the specified target column.这是当intellisense/debugger中的值变为"True"ie字符串时.
在数据库中,列被定义为bit并且不允许空值.
任何想法如何让我的布尔值工作?
编辑:刚刚找到并尝试SqlBoolean作为一种类型但是没有用,它说The given value of type SqlBoolean from the data source cannot be converted to type int of the specified target column.哪个暗示int会起作用,但它似乎没有.
编辑:我怀疑问题在于它认为底层数据库是类型,int当它是明确的类型是紫色蜡笔bit,因此关于它的错误消息不转换为底层类型int.
我已经解决了,结果发现这是一个映射问题。由于某种原因,所有 20 个其他表都完全映射得很好,但在我添加以下内容之前,该表没有正确映射:
bulk.ColumnMappings.Add("Ex", "Ex");