Multiple Result Sets in ADO.net using SqlClient
2008-06-19 21:28
423 查看
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.
Don't for get to reference name space
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.
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.
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.
相关文章推荐
- Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
- Ado.net Querying the Database for Multiple Result Sets(多数据结果集)
- Ado.net Querying the Database for Multiple Result Sets(多数据结果集)
- Previewing and plotting multiple sheets in AutoCAD using .NET
- StoreGeneratedPattern Property in ADO.NET Entity Model Designer sets CDSL annotation but not SSDL attribute.
- o/r mapping in ado.net using DataSet
- Upload Multiple Files in ASP.NET using jQuery
- Saving Parent-child Data in a Multitiered App Using ADO.NET
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- ADO.NET2.0 Querying Large Result Sets Asynchronously(ADO.NET 异步操作)
- Prevent Multiple Logins Using the Cache in ASP.NET
- CRUD Operations In ASP.NET MVC 5 Using ADO.NET
- VB .NET 2003 using datagridTableStyles on multiple tables in a data grid
- ADO.NET2.0 Querying Large Result Sets Asynchronously(ADO.NET 异步操作)
- VB .NET 2003 using datagridTableStyles on multiple tables in a data grid
- 【转载】在ASP.NET 2.0 中如何使用多个Sitemap文件【Using Multiple Sitemap Files in ASP.NET 2.0--英文】
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- using swfUpload in asp.net mvc
- 基于多活动结果集(Multiple Active Result Sets,简称MARS)的C#示例分析
- Learning ADO.NET 3.5 Cookbook:(2) Synchronizing Master-Detail Data in a Windows Forms Application