ADO.NET 2.0 大批量数据操作和多个动态的结果集

本文介绍如何使用SqlBulkCopy类批量导入数据到SQL Server,并演示了使用MultipleActiveResultSets特性同时处理多个DataReader对象的方法。

1.大批量数据操作
可以利用SqlBulkCopy类快速写入大批量数据,针对SQL Server的优化,可以写入DataRow数据,DataTable,DataReader
WriteToServer(DataTable)写入数据表
WriteToServer(DataRow[])批次写入数据行
WriteToServer(DataTable ,DataRowState)按行状态写入数据库表
WriteToServer(IDataReader)写入DataReader对象
None.gif  string connstr = "server=(local);database=northwind;integrated security=true;async=true";
None.gif            
// Fill up a DataSet
None.gif
            DataSet ds = new DataSet();
None.gif            SqlConnection conn 
= new SqlConnection(connstr);
None.gif            SqlDataAdapter dadp 
= new SqlDataAdapter("select * from customers", conn);
None.gif            dadp.Fill(ds);
None.gif            
// Copy the Data to SqlServer
None.gif
            SqlBulkCopy bcp = new SqlBulkCopy(connstr);
None.gif            bcp.DestinationTableName 
= "customers1";
None.gif            bcp.WriteToServer(ds.Tables[
0]);

2.多个动态的结果集
Multiple Active Result Sets(MARS)
这个只能在SQL Server 2005中使用
可以在一个Command对象上同时打开多个DataReader
None.gif string connstr = "server=(local);database=northwind;integrated security=true;async=true";
None.gif            SqlConnection conn 
= new SqlConnection(connstr);
None.gif            conn.Open();
None.gif            SqlCommand cmd1 
= new SqlCommand("select * from customers", conn);
None.gif            SqlCommand cmd2 
= new SqlCommand("select * from orders", conn);
None.gif            SqlDataReader rdr1 
= cmd1.ExecuteReader();
None.gif            
// next statement causes an error prior to SQL Server 2005
None.gif
            SqlDataReader rdr2 = cmd2.ExecuteReader();
None.gif            
// now you can reader from rdr1 and rdr2 at the same time.



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/30/264851.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值