Home  >  Article  >  Database  >  ADO.NET 2.0批量数据操作和多动态结果集

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

WBOY
WBOYOriginal
2016-06-07 15:04:331143browse

1.大 批量 数据 操作 可以利用SqlBulkCopy类快速写入大 批量 数据 ,针对SQL Server的优化,可以写入DataRow 数据 ,DataTable,DataReader WriteToServer(DataTable)写入 数据 表 WriteToServer(DataRow[])批次写入 数据 行 WriteToServer(DataTable ,

  1.大批量数据操作

  可以利用SqlBulkCopy类快速写入大批量数据,针对SQL Server的优化,可以写入DataRow数据,DataTable,DataReader

  WriteToServer(DataTable)写入数据

  WriteToServer(DataRow[])批次写入数据

  WriteToServer(DataTable ,DataRowState)按行状态写入数据库表

  WriteToServer(IDataReader)写入DataReader对象

ADO.NET 2.0批量数据操作和多动态结果集  string connstr = "server=(local);database=northwind;integrated security=true;async=true";
ADO.NET 2.0批量数据操作和多动态结果集            
// Fill up a DataSet
ADO.NET 2.0批量数据操作和多动态结果集
            DataSet ds = new DataSet();
ADO.NET 2.0批量数据操作和多动态结果集            SqlConnection conn 
= new SqlConnection(connstr);
ADO.NET 2.0批量数据操作和多动态结果集            SqlDataAdapter dadp 
= new SqlDataAdapter("select * from customers", conn);
ADO.NET 2.0批量数据操作和多动态结果集            dadp.Fill(ds);
ADO.NET 2.0批量数据操作和多动态结果集            
// Copy the Data to SqlServer
ADO.NET 2.0批量数据操作和多动态结果集
            SqlBulkCopy bcp = new SqlBulkCopy(connstr);
ADO.NET 2.0批量数据操作和多动态结果集            bcp.DestinationTableName 
= "customers1";
ADO.NET 2.0批量数据操作和多动态结果集            bcp.WriteToServer(ds.Tables[
0]);

  2.多个动态结果

  Multiple Active Result Sets(MARS)

  这个只能在SQL Server 2005中使用

  可以在一个Command对象上同时打开多个DataReader

ADO.NET 2.0批量数据操作和多动态结果集 string connstr = "server=(local);database=northwind;integrated security=true;async=true";
ADO.NET 2.0批量数据操作和多动态结果集            SqlConnection conn 
= new SqlConnection(connstr);
ADO.NET 2.0批量数据操作和多动态结果集            conn.Open();
ADO.NET 2.0批量数据操作和多动态结果集            SqlCommand cmd1 
= new SqlCommand("select * from customers", conn);
ADO.NET 2.0批量数据操作和多动态结果集            SqlCommand cmd2 
= new SqlCommand("select * from orders", conn);
ADO.NET 2.0批量数据操作和多动态结果集            SqlDataReader rdr1 
= cmd1.ExecuteReader();
ADO.NET 2.0批量数据操作和多动态结果集            
// next statement causes an error prior to SQL Server 2005
ADO.NET 2.0批量数据操作和多动态结果集
            SqlDataReader rdr2 = cmd2.ExecuteReader();
ADO.NET 2.0批量数据操作和多动态结果集            
// now you can reader from rdr1 and rdr2 at the same time.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn