我想通过参数传递特定数据,制作一个可用于所有批量插入的SqlBulkCopy方法.
现在我需要对其中一些进行映射.我不知道如何制作SqlBulkCopyColumnMappingCollection,因为那是我计划传递映射集合并使用它.但是我不知道如何制作它.我不能成为它的新对象.
这就是我现在拥有的.如何添加它做映射把它传递进去?
public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize)
{
// Get the DataTable
DataTable dtInsertRows = dataTable;
using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
{
sbc.DestinationTableName = DestinationTbl;
// Number of records to be processed in one go
sbc.BatchSize = batchSize;
// Finally write to server
sbc.WriteToServer(dtInsertRows);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用SqlBulkCopy两个不同列的SQL Server 2008(将一些数据从prod服务器移动到dev).所以想要跳过一些尚未存在/尚未删除的列.
我怎样才能做到这一点?一些技巧ColumnMappings?
编辑:
我做下一个:
DataTable table = new DataTable();
using (var adapter = new SqlDataAdapter(sourceCommand))
{
adapter.Fill(table);
}
table.Columns
.OfType<DataColumn>()
.ForEach(c => bulk.ColumnMappings.Add(
new SqlBulkCopyColumnMapping(c.ColumnName, c.ColumnName)));
bulk.WriteToServer(table)
Run Code Online (Sandbox Code Playgroud)
得到:
给定的ColumnMapping与源或目标中的任何列都不匹配.
在数据库中:存在一个字段,其类型为TINYINT(1),默认值为0。在Model中,其定义为TINYINT(1)DEFAULT0。但是,它给出如下错误:
错误
[jar:file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/NetaCommerceFrameworkAdmin/WEB-INF/lib/NetaCommerceFramework-0.0.1-SNAPSHOT.jar!/com/netasoft/commerce/framework/lang/dao/LangDaoImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type
[org.hibernate.SessionFactory]: :Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/admin-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in kaft.nc_alert_log for column alerted. Found: bit, expected: TINYINT(1) DEFAULT 0; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/admin-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in kaft.nc_alert_log for column …Run Code Online (Sandbox Code Playgroud)