///
/// 把DataTable中的数据批量写入到SQL SERVER表中
///
/// DataTable内存表
/// 数据库连接字符串
/// 数据库表名
public static void DataTableToSqlServer(DataTable dt,string connectString,string SqlserverTableName)
{
string connectionString = connectString;
using (SqlConnection destinationConnection=new SqlConnection())
{
destinationConnection.Open();
using (SqlBulkCopy bulkCopy=new SqlBulkCopy(destinationConnection))
{
try
{
bulkCopy.DestinationTableName = SqlserverTableName;//要插入的表的表名字
bulkCopy.BatchSize = dt.Rows.Count;
int y = dt.Columns.Count;//内存表中的列数
bulkCopy.ColumnMappings.Add("FromSubinv", "FromSubinv");//映射字段名DataTable列名,数据库对应的列名
}
catch (Exception ex)
{
throw ex;
}
}
}
}
把DataTable中的数据批量写入到SQL SERVER表中
原文:https://www.cnblogs.com/zhujie-com/p/12310217.html