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

  • 来源: 互联网 作者: rocket   2008-03-17/14:35
  • 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.


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多