如何在不同的数据集中获取存储过程的批处理查询

我正在尝试从单个页面中的不同不同数据集的一个存储过程中获取所有数据。

CREATE PROCEDURE [dbo].[usp_Details](@status int, @Id int) AS begin Select u.Id,u.FName,u.ImageName,u.ImagePath,u.Sex FROM [User] as u where u.Id IN (SELECT MyId as Id FROM Friends WHERE FriendId=@Id AND FriendStatus=0) Select Points,FName from [User] where Id=@Id SELECT ImagePath from [User] where Id=@Id end GO 

现在,我如何为单个查询绑定datatable / dataset。 示例:query1表示dataset1,query2表示dataset2,query3表示dataset3

如果这是不可能的,那么每次获取不同表时避免连接数据库的最佳方法是哪种方法。

不确定您是否可以使用数据集执行此操作,但您始终可以回退到较低的杠杆ADO.NET API。 SqlReader通过NextResult方法支持多个结果集。

这是一种方式。

您可以轻松地移回DataTables数组,而无需将它们包装在DataSet中。 从资源的角度来看更好,因为DataSet是一个非常重量级的类。 DataSet几乎是一个内存中的关系数据库,它为构建DataTables之间的关系和约束的实施提供了各种支持。

根本不要求DataTable成为DataSet的一部分。 它基本上只是一个容器。

这是代码:

 static DataSet[] ExecStoredProcedure() { DataSet[] instance = null ; const string credentials = "Data Source=localhost;Initial Catalog=sandbox;Integrated Security=SSPI;" ; using ( SqlConnection connection = new SqlConnection( credentials ) ) using ( SqlCommand command = connection.CreateCommand() ) using ( SqlDataAdapter adapter = new SqlDataAdapter( command ) ) using ( DataSet results = new DataSet() ) { command.CommandType = CommandType.StoredProcedure ; command.CommandText = @"someStoredProcedure" ; connection.Open() ; adapter.Fill( results ) ; connection.Close() ; List list = new List( results.Tables.Count ) ; while ( results.Tables.Count > 0 ) { DataTable dt = results.Tables[0] ; results.Tables.RemoveAt(0) ; DataSet ds = new DataSet() ; ds.Tables.Add( dt ) ; list.Add(ds) ; } instance = list.ToArray() ; } return instance ; } 

执行返回多个结果集的存储过程的另一个选项,将指定的结果集作为DataTable返回。 达到所需的结果集时,存储过程的执行会短路:

 static DataTable ExecStoredProcedure( string someStoredProcedure , int desiredTableNumber ) { DataTable instance = null ; const string credentials = "Data Source=localhost;Initial Catalog=sandbox;Integrated Security=SSPI;" ; using ( SqlConnection connection = new SqlConnection( credentials ) ) using ( SqlCommand command = connection.CreateCommand() ) { command.CommandType = CommandType.StoredProcedure ; command.CommandText = someStoredProcedure ; connection.Open() ; using ( SqlDataReader reader = command.ExecuteReader() ) { int i = 0 ; while ( reader.HasRows ) { // toss any unwanted results ; if ( instance != null ) instance.Dispose() ; // toss unwanted results instance = new DataTable() ; instance.Load( reader ) ; if ( i == desiredTableNumber || reader.IsClosed ) break ; ++i ; } if ( i != desiredTableNumber ) { if ( instance != null ) instance.Dispose() ; instance = null ; } // try to tidy up so the connection isn't hose, if we short-circuited execution command.Cancel() ; reader.Close() ; } command.Cancel() ; connection.Close() ; } return instance ; } 

我得到了答案,但仍然需要一个建议,这是否值得使用。

 private void GetMultiSelect() { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnect1"].ConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = "usp_AllDetail"; cmd.CommandType = CommandType.StoredProcedure; SqlParameter sqlParam = cmd.Parameters.Add("@Id", SqlDbType.Int, 4); sqlParam.Value = 1; //dataset object to get all select statement results DataSet ds = new DataSet(); //sql dataadoptor to fill dataset using (SqlDataAdapter adp = new SqlDataAdapter(cmd)) { //here all select statements are fill in dataset object adp.Fill(ds); //now u can fetch each and every select statement by providing table index in dataset foreach (DataTable dt in ds.Tables) { //select statement result in dt.. } //or instead of loop u can specify the index GridView1.DataSource= ds.Tables[1]; // first select statement result GridView1.DataBind(); GridView2.DataSource = ds.Tables[0]; // second select statement result GridView2.DataBind(); } } } }