public QueryResult SqlBulkCopy(DataTable dt, string tableName)
{
try
{
connection.Open();
using (var copy = new SqlBulkCopy(connection))
{
copy.DestinationTableName = tableName;
copy.WriteToServer(dt);
}
return new QueryResult() { result = true };
}
catch (Exception ex)
{
return new QueryResult() { result = false, msg = ex.Message };
}
finally { connection.Close(); }
}
使用
private static void InsertTestData()
{
var table = new DataTable();
var sqlOperation = new SqlOperation();
for (int i = 1; i <= 12; i++)
{
table.Columns.Add(new DataColumn("column1" + i, typeof(string)));
}
table.Columns.Add(new DataColumn("CreateAt", typeof(DateTime)));
for (int j = 0; j < 10; j++)
{
for (int i = 1; i <= 10000; i++)
{
table.Rows.Add(new object[] {
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
Guid.NewGuid().ToString().Replace("-","-"),
DateTime.Now
});
}
var result = sqlOperation.SqlBulkCopy(table, "TestTable");
table.Rows.Clear();
}
}
标签:datetable,QueryResult,connection,new,var,ado,net,copy
From: https://www.cnblogs.com/duixue/p/18231187