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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值