`
ihuashao
  • 浏览: 4665910 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Multiple Result Sets in ADO.net using SqlClient

阅读更多
Introduction
In today’s world most of the applications are data centric. Every form either windows or web has tons and tons of data to de displayed; the major chunk of the data is read only or for look up purpose. To get data from the database to the client,a good amount of resource are used up for establishing database connection; too many round trip calls to the server and there by increasing the burden on the backend servers and significant increase in network traffic. . Net has many cool features for SQL server 2000 to improve the over all scalability of the applications. We will see one of the cool futures today; retrieve multiple result sets in a single SqlDataReader. This feature is available only for SQL server, why not for Oracle?, well it’s any one’s guess. Anyway the same results can be achieved even for Oracle through other means; which we will discuss some time later. Well. It’s time to get to business. Let us open new ASP.Net and place two DropDownList and a DataGrid.
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="cboCategory" runat="server"></asp:DropDownList>
<asp:DropDownList id="cboOrder" runat="server"></asp:DropDownList>
<asp:DataGrid id="DataGridProd" runat="server"></asp:DataGrid>
</form>
Don't for get to reference name space
using System.Data.SqlClient;
Let us examine the code step by step. Step#1 : Connect to Database Step#2 : Club all the Sql statements in to one string separated by “;” and pass into SQLCommand object Step#3 : Execute the SqlComman and capture the results into Sqldata reader Step#4 : Bind Data to corresponding server controls Step#5 : Use the NextResult()method of DataReader to get the next result set. Step#6 : Close the SqlDataReader and Database connection.
private void Page_Load(object sender, System.EventArgs e)
 {
  if (!IsPostBack){GetData();}
 // Put user code to initialize the page here
 }


 private void GetData()
 {
 //Step#1 : Database Connection
 SqlConnection con=new SqlConnection("data source=home-pc;initial 
 catalog=Northwind;User id="":Password="";);
 con.Open();
 //Step#2 : Pass multiple sql statements
 string sql="select * from categories;
 select * from orders;select top 5 * from products;";
 //Step#3 : Execute Command 
 SqlCommand cmd=new SqlCommand(sql,con);
 SqlDataReader Rd = cmd.ExecuteReader();

 //Step#4 : Categories
 cboCategory.DataSource=Rd;
 cboCategory.DataTextField="CategoryName";
 cboCategory.DataValueField="CategoryID";
 cboCategory.DataBind();
 //Step#5 : Move to next result set
 Rd.NextResult();		

 //Orders
 cboOrder.DataSource=Rd;
 cboOrder.DataTextField="ShipName";
 cboOrder.DataValueField="Orderid";
 cboOrder.DataBind();
 //Move to next result set
 Rd.NextResult();

 //Products
 DataGridProd.DataSource=Rd;
 DataGridProd.DataBind();
 //Step # 6:Close Data reader
 Rd.Close();
 //close connection
 con.Close();
		}
Note: Do not use the CommandBehavior.CloseConnection attribute with ExecuteReader() methord of SqlCommand object.This will cause an error. Each time we use the NextResult() method of SQLDataReader internally commandobject executes the next sql statement and fetches the next result set.
Conclusion
This is one of many things we have been missing for years; .net has cool stuff like this one, to cut short the development time. You can also use Dataset in the similar fashion.Whether to go for Dataset or Datareader depends on the architecture of the project and the business requirement.
分享到:
评论

相关推荐

    ADO.NET 全面解析ADO.NET

    ADO.NET是Microsoft开发的一种用于访问关系数据库的数据访问技术,它为.NET Framework应用程序提供了一套全面的数据访问组件。本文将深入探讨ADO.NET的核心概念、对象模型和数据操作。 首先,我们来看ADO.NET的设计...

    ado.net面试题

    8. **管理提供者**:针对不同数据库系统,ADO.NET 有不同的管理提供者,如SQL Server 的`SqlClient`提供者和用于其他OLE DB兼容数据库的`OleDbClient`提供者。 在面试中,可能被问及的问题包括但不限于: - ADO.NET...

    ADO.NET本质论.rar

    ADO.NET支持多种数据库,如SQL Server、Oracle、MySQL等,通过提供不同的Provider(如SqlClient、OleDb等)实现与特定数据库系统的交互。这些Provider隐藏了与特定数据库通信的细节,使开发者能够使用统一的API来...

    ADO.NET本质论.pdf

    讲解了数据结构,演示了如何用ado.net来解决具体的数据访问问题。重点讨论了ado.net如何有效地平衡"功能的泛化"和"执行效率",以及它如何解决对扩展性、并发性和可靠性的要求。针对其他数据访问api(包括ole db,ado...

    ADO.net操作数据库总结

    ADO.net 操作数据库总结 ADO.net 是 một 微软公司开发的数据库访问技术,用于与关系数据库管理系统(RDBMS)交互。以下是 ADO.net 操作数据库的总结,包括使用 SqlConnection 和 SqlCommand 对象连接数据库、执行...

    ADO.NET 4 入门到精通源代码

    ADO.NET 4 是微软开发的一款用于访问数据库的强大框架,它为.NET Framework应用程序提供与各种数据库交互的能力。在ADO.NET 4中,微软引入了多项改进和新特性,以提升性能、简化开发流程并增强可扩展性。这个"ADO...

    ADO.NET资料-demo程序

    7. 错误处理和事务管理:在ADO.NET中,可以捕获和处理System.Data.SqlClient.SqlException类的异常,以及通过Transaction对象进行事务控制,确保数据操作的原子性和一致性。 通过这个“ADO.NET资料-demo程序”,你...

    ADO .NET2.0技术内幕

    ADO.NET 2.0是微软.NET Framework 2.0中的数据访问组件,它提供了一组丰富的类库,用于与各种数据库进行交互。这个技术内幕文档很可能是深入解析ADO.NET 2.0特性和用法的详细教程。在本文中,我们将深入探讨ADO.NET ...

    ADO.NET 2.0试题 答案

    在ADO.NET 2.0中,开发人员可以使用特定于数据库的数据提供程序(如SqlClient for SQL Server)来创建Connection对象,以便打开、管理和关闭数据库连接。 2. **Command对象**:用于执行SQL查询或存储过程。开发人员...

    ADO.NET技术内幕rar

    这部分可能详细阐述ADO.NET如何作为连接数据库的桥梁,如何处理数据提供者(如SqlClient、OleDb等)以及DataSet、DataTable等核心组件的作用。 第二部分可能深入到DataSet和DataTable的使用,这两个组件是ADO.NET中...

    ADO.NET微软认证题库及其复习资料

    ADO.NET是微软开发的一种数据访问技术,用于在.NET应用程序中高效地处理数据库。这个压缩包包含的资源专门针对ADO.NET的微软认证考试,是备考的重要参考资料。以下将详细阐述ADO.NET的关键知识点,以及如何利用这些...

    Ado.net 经典(附数据库)

    ADO.NET支持多种数据库,如SQL Server、Oracle、MySQL等,每种数据库都有相应的数据提供者,如SqlClient(用于SQL Server)、OleDb(通用的OLE DB提供者)、Odbc(通用的ODBC提供者)。连接字符串的构建根据不同的...

    ado.net官方教程

    1. **数据提供者模型**:ADO.NET的核心是数据提供者模型,它包括SQL Server数据提供者(System.Data.SqlClient)、Oracle数据提供者(System.Data.OracleClient)、ODBC数据提供者(System.Data.Odbc)和OLE DB数据...

    ADO.NET 2.0高级编程

    ADO.NET 2.0是微软.NET Framework的一部分,它提供了一组全面的数据访问组件,用于与各种数据源交互,包括数据库、XML文档和其他数据存储。在ADO.NET 2.0高级编程中,开发者可以深入理解如何高效地利用这些组件来...

    Sybase ADO.NET2 驱动

    使用Sybase ADO.NET 2驱动,开发者可以利用System.Data.SqlClient命名空间中的类,如SqlConnection、SqlCommand、SqlDataAdapter等,来创建数据库连接、执行SQL命令、填充数据集等。同时,该驱动还支持使用.NET的...

    ADO.NET高级编程.rar

    1. **数据提供者模型**:ADO.NET的核心概念之一是数据提供者模型,包括.NET Framework Data Provider for SQL Server(SqlClient)、Oracle Data Provider for .NET(ODP.NET)、ODBC和OLE DB等。每个数据提供者都有...

    與您息息相關的 ADO.NET

    为了与特定的数据库系统交互,ADO.NET提供了多种数据提供者,如SQL Server提供者(System.Data.SqlClient命名空间)和OleDb提供者(System.Data.OleDb命名空间)。SQL Server提供者针对SQL Server进行了优化,包括智能...

    ADO.NET 应用程序中实现和获得最佳性能

    首先,ADO.NET包含了.NET框架数据提供程序,如SQL Server数据提供程序(SqlClient),它们负责与特定数据库引擎通信。了解不同提供程序的优势和限制,可以帮助你选择最适合项目需求的数据提供程序。 DataSet和...

Global site tag (gtag.js) - Google Analytics