DataReader的NextResult方法处理多个数据集

下面的例子我们用存储过程返回两个数据集,然后用DataReaderNextResult方法处理这两个数据集。

存储过程的代码(名称:Proc):

CREATE PROC Proc -Multiple Resultsets

AS

SELECT * FROM Users

SELECT * FROM Users WHERE State = CA

GO

取这两个数据集的代码:

注意DataReaderNextResult出现的地方

String ConnString = "User ID=sa;password=sa;Initial Catalog=pubs;Data Source=myServer";SqlConnection Connection = new SqlConnection(myConnString);SqlCommand Command = new SqlCommand();SqlDataReader reader ;Command.CommandType = CommandType.StoredProcedure;Command.Connection = Connection;Command.CommandText = "Proc";int RecordCount=0;try{Connection.Open();reader = command.ExecuteReader();int RecordCount=0;// read the data from that resultset while (reader.Read()){RecordCount = RecordCount + 1;}Response.Write("Total number of Users:" + RecordCount.ToString());// read the next resultset reader.NextResult();RecordCount = 0;// read the data from that second resultset while (reader.Read()){RecordCount = RecordCount + 1;}Response.Write("Total number of Users from California:" + RecordCount.ToString());}catch (Exception ex){MessageBox.Show(ex.ToString());}finally{Connection.Close();}

 


 

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据