/// <summary>
/// 批量保存多表
/// </summary>
/// <param name="dt1"></param>
/// <param name="TableName"></param>
/// <returns></returns>
public static void SqlBatchCopy(DataTable dt, string TableName,DataTable dt1, string TableName1)
{
using (SqlTransaction st = Con.BeginTransaction())
{
using (SqlBulkCopy copy = new SqlBulkCopy(Con, SqlBulkCopyOptions.Default, st))
{
for (int i = 0; i < dt.Columns.Count; i++)
{
copy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
}
copy.DestinationTableName = TableName;
copy.WriteToServer(dt);
}
using (SqlBulkCopy copy = new SqlBulkCopy(Con, SqlBulkCopyOptions.Default, st))
{
for (int i = 0; i < dt1.Columns.Count; i++)
{
copy.ColumnMappings.Add(dt1.Columns[i].ColumnName, dt1.Columns[i].ColumnName);
}
copy.DestinationTableName = TableName1;
copy.WriteToServer(dt1);
}
st.Commit();
}
}sql
//簡單測試數據庫
DataTable tb = new DataTable();
tb.Columns.Add( "Cname1", typeof (string));
DataRow dr = tb.NewRow();
for (int i = 1; i <= 10000; i++)
{
dr = tb.NewRow();
dr[ "Cname1"] = "測試" + i;
tb.Rows.Add(dr);
}服務器
System.Diagnostics. Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
using (System.Data.SqlClient.SqlBulkCopy copy = new System.Data.SqlClient.SqlBulkCopy ("server=.;database=TestA;uid=sa;pwd=sasa"))
{
copy.ColumnMappings.Add( "Cname1", "Cname" );
copy.DestinationTableName = "TbA";
copy.WriteToServer(tb);
}
st.Stop();
MessageBox.Show( "新增成功,耗時" + st.ElapsedMilliseconds);併發
- class Program
- {
- static volatile bool result;
- static void Main(string[] args)
- {
- DataSet ds = ExportDataSet();
-
-
- Thread t = new Thread(delegate() { result = Insert(ds); Console.WriteLine(result ? "導入成功" : "導入失敗"); });
- t.Start();
- Thread t1 = new Thread(delegate() { result = Insert(ds); Console.WriteLine(result ? "導入成功" : "導入失敗"); });
- t1.Start();
- Console.ReadLine();
- }
-
-
-
-
-
-
- static private DataSet ExportDataSet()
- {
-
-
- SqlConnection RemoteConn = new SqlConnection("Data Source=192.168.0.183;Initial Catalog=Northwind;User ID=sa;Password=sa");
-
-
-
- using (
-
-
- SqlDataAdapter oda = new SqlDataAdapter("SELECT [OrderID], [CustomerID], [EmployeeID], [ShipCountry] FROM [Northwind].[dbo].[Orders]", RemoteConn))
-
-
-
-
- {
- DataSet ds = new DataSet();
-
- oda.Fill(ds, "Orders");
-
- return ds;
-
- }
-
- }
-
-
-
-
-
-
- public static bool Insert(DataSet ds)
- {
-
-
- using (SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=sa"))
- {
- sqlconn.Open();
- SqlTransaction sqlbulkTransaction = sqlconn.BeginTransaction(IsolationLevel.ReadCommitted);
-
-
-
- using (SqlBulkCopy sbc = new SqlBulkCopy(sqlconn, SqlBulkCopyOptions.KeepIdentity, sqlbulkTransaction))
- {
- sbc.BatchSize = 20000;
- sbc.BulkCopyTimeout = 50;
- if (ds.Tables == null || ds.Tables.Count == 0)
- return false;
-
- if (ds.Tables.Count == 1)
- {
-
-
- return BulkInsert(sbc, ds.Tables[0], sqlbulkTransaction); ;
-
- }
- else
- {
- bool res = true;
- foreach (DataTable dt in ds.Tables)
- {
-
- res = BulkInsert(sbc, dt, sqlbulkTransaction);
-
- }
- return res;
- }
-
- }
- }
-
- }
-
- private static bool BulkInsert(SqlBulkCopy sbc, DataTable dt, SqlTransaction sqlbulkTransaction)
- {
-
- bool res = true;
- try
- {
-
-
- sbc.DestinationTableName = dt.TableName;
-
-
-
-
- for (int i = 0; i < dt.Columns.Count; i++)
- {
-
- sbc.ColumnMappings.Add(i,i);
- }
-
- sbc.WriteToServer(dt);
-
-
- sqlbulkTransaction.Commit();
- res = true;
- }
- catch (SqlException ex)
- {
- res = false;
- sqlbulkTransaction.Rollback();
- }
-
- return res;
- }
- }