dt信息中包含数据和表名
public static void SqlBulkInsert(DataTable dt, string connStr)
{
try
{
using (var conn = new SqlConnection(connStr))
{
try
{
conn.Open();
using (var bulkcopy = new SqlBulkCopy(connStr,
SqlBulkCopyOptions.UseInternalTransaction))
{
//批量插入跟踪日志至对应的日志表
bulkcopy.DestinationTableName = dt.TableName;
for (int i = 0; i < dt.Columns.Count; i++)
{
bulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
}
bulkcopy.WriteToServer(dt);
}
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}
catch (Exception ex)
{
other.WriteFile("SqlBulkCopy插入数据异常:" + ex);
}
}