1.大批量數據操作
可以利用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]);
2.多個動態的結果集
Multiple Active Result Sets(MARS)
這個只能在SQL Server 2005中使用
可以在一個Command對象上同時打開多個DataReader
string connstr = "server=(local);database=northwind;integrated security=true;async=true";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd1 = new SqlCommand("select * from customers", conn);
SqlCommand cmd2 = new SqlCommand("select * from orders", conn);
SqlDataReader rdr1 = cmd1.ExecuteReader();
// next statement causes an error prior to SQL Server 2005
SqlDataReader rdr2 = cmd2.ExecuteReader();
// now you can reader from rdr1 and rdr2 at the same time.