批量导入数据库
时间:2021-07-01 10:21:17
帮助过:26人阅读
bool MsSqlBulkCopyData(DataTable dt,
string connectionString)
{
bool result;
try
{
using (SqlConnection conn =
new SqlConnection(connectionString))
{
conn.Open();
SqlTransaction trans =
conn.BeginTransaction();
SqlBulkCopy sqlbulkCopy =
new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans);
sqlbulkCopy.DestinationTableName =
dt.TableName;
sqlbulkCopy.BulkCopyTimeout =
1000;
foreach (DataColumn dtColumn
in dt.Columns)
{
sqlbulkCopy.ColumnMappings.Add(dtColumn.ColumnName, dtColumn.ColumnName);
}
try
{
sqlbulkCopy.WriteToServer(dt);
trans.Commit();
result =
true;
}
catch
{
trans.Rollback();
sqlbulkCopy.Close();
result =
false;
}
finally
{
sqlbulkCopy.Close();
conn.Close();
}
}
}
catch (Exception e)
{
this.Logger.WriteLog(
"-----------利用Net SqlBulkCopyData 批量导入数据库,速度超快-----------\r\n" + e.Message +
"\r\n");
result =
false;
}
return result;
}
批量导入数据库
标签:nal commit rollback 导入数据 res option out cat copy