2012-02-16 13 views
0

1つのページに異なるデータセットごとに1つのストアドプロシージャからすべてのデータをフェッチしようとしています。異なるデータセットでストアドプロシージャのバッチクエリを取得する方法

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 [email protected] AND FriendStatus=0) 

Select Points,FName from [User] where [email protected] 

SELECT ImagePath from [User] where [email protected] 
end 
GO 

ここで、個々のクエリでデータテーブル/データセットをバインドする方法を説明します。 例:dataset1の場合はquery1、dataset2の場合はquery2、dataset3の場合はquery3

これが可能でない場合は、異なるテーブルをフェッチするたびにデータベースを接続しないようにする最善の方法です。

答えて

0

私は答えを得ましたが、まだこの価値があるかどうかの提案が必要です。

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(); 
      } 
     } 
    } 
} 
0

データセットを使用できるかどうかはわかりませんが、下部レバーのADO.NET APIにはいつでも戻ってください。 SqlReaderは、NextResultメソッドを使用して複数の結果セットをサポートします。

0

ここには1つの方法があります。

DataSetで配列をラップせずにDataTableの配列を簡単に戻すことができます。リソースの観点からは、DataSetはかなり重いクラスなので、より優れています。 DataSetは、DataTableと制約の適用との間の関係を構築するためのあらゆる種類のサポートを提供する、かなりメモリ内のリレーショナルデータベースです。

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<DataSet> list = new List<DataSet>(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 ; 
} 

複数の結果セットを返すストアドプロシージャを実行するための別のオプション:

は、ここでは、コードです。

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 ; 
} 
+0

:ありがとうございました。しかし、あなたの上記の例では、特定のクエリのデータセットを埋めるようには見えません。ストアドプロシージャには3つの "select"ステートメントがありますので、それぞれ異なるデータセットまたはデータテーブルを入力したいと思います。 –

+0

私の修正された答えを見てください。それが価値あるものについては、これで何を達成しようとしていますか?データベース接続は、デフォルトでキャッシュされます。接続を確立した後は、すでに開かれている接続をキャッシュから取得するだけで、後続の「開く」は安価です。さらに、5の結果セット3にのみ関心がある場合は、捨てられるデータベースで余分な作業をたくさんしています。 –

関連する問題