大批量數據操作
可以利用SqlBulkCopy類快速寫入大批量數據,針對SQL Server的優化,可以寫入DataRow數據,DataTable,DataReader
WriteToServer(DataTable)寫入數據表
WriteToServer(DataRow[])批次寫入數據行
WriteToServer(DataTable ,DataRowState)按行狀態寫入數據庫表
WriteToServer(IDataReader)寫入DataReader對象
string connstr = "server=(local);database=northwind;integrated security=true;async=true";
// Fill up a DataSet
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstr);
SqlDataAdapter dadp = new SqlDataAdapter("select * from customers", conn);
dadp.Fill(ds);
// Copy the Data to SqlServer
SqlBulkCopy bcp = new SqlBulkCopy(connstr);
bcp.DestinationTableName = "customers1";
bcp.WriteToServer(ds.Tables[0]);