2013-08-29 69 views
9

我有一個ASP經典系統轉換爲C#返回從存儲過程的多個記錄在C#

我有一個存儲過程,最多可以返回記錄集7(根據傳入的參數)。

我需要知道如何才能簡單地將所有記錄集作爲單獨的DataTables返回,這樣我就可以遍歷所有的記錄集,並在不必運行多個SQL語句的情況下跳到下一個DataTable,使用多個adapter.Fill語句將每個表添加到DataSet中。

在經典中,它是一個簡單的Do而不是objRS.EOF循環與objRS.NextRecordset(),當我到達循環結束時移動到下一個語句。

有什麼我可以使用,不需要全部重寫當前的後端代碼?

每個記錄集具有不同數量的列和行。他們彼此不相關。我們從Stored Proc返回多個記錄集以減少流量。

例子會不錯。

感謝

回答

11
SqlConnection con=new SqlConnection("YourConnection String"); 
SqlCommand cmd=new SqlCommand(); 
SqlDataAdapter da=new SqlDataAdapter(); 
DataSet ds = new DataSet(); 
cmd = new SqlCommand("name of your Stored Procedure", con); 
cmd.CommandType = CommandType.StoredProcedure; 
//cmd.Parameters.AddWithValue("@SuperID", id);//if you have parameters. 
da = new SqlDataAdapter(cmd); 
da.Fill(ds); 
con.Close(); 

在此之後,您可以將廣告使用不同的(7)記錄集的優勢

ds.Tables[0] 
ds.Tables[1] 
ds.Tables[2] 
ds.Tables[3] 
ds.Tables[4] 
ds.Tables[5] 
ds.Tables[6] 
+0

謝謝,我喜歡這個答案,純粹是因爲我已經有一個包裝函數的數據類來處理連接,並在類被銷燬時處理它們等。我只是想使用一個現有的函數我有和參考數據集內的表。 – user2334626

5

這將返回你所有你需要

using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString)) 
{ 
    using (SqlCommand cmd = new SqlCommand()) 
    { 
     cmd.CommandText = "yoursp"; 
     cmd.Connection = conn; 
     cmd.CommandType = CommandType.StoredProcedure; 

     conn.Open(); 

     SqlDataAdapter adapter = new SqlDataAdapter(cmd); 

     DataSet ds = new DataSet(); 
     adapter.Fill(ds); 

     conn.Close(); 
    } 
} 
+2

這是我用來從存儲過程獲取多個記錄集。爲了澄清,記錄集存儲在'ds.Tables'中,您可能希望在開始處放置'DataSet ds = new DataSet();'以便您可以在'using(SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))'塊。只是一個想法。 – JoeFletch

+0

是的,你可以將它移出。只是舉一個如何去做的例子。 – Ehsan

8

如果您填寫使用​​方法再一個DataSet從存儲過程返回的每個記錄集都將返回內的數據表的數據集

DataSet dataset = new DataSet(); 
using (var adapter = new SqlDataAdapter("yourStoredProcedure", yourConnectionString)) 
{ 
    adapter.SelectCommand.CommandType = CommandType.StoredProcedure; 
    adapter.Fill(dataset); 
} 
for (int i = 0; i < dataset.Tables.Count; i++) 
{ 
    // Do something for each recordset 
} 

如果您使用SqlDataReader,然後使用可以使用SqlDataReader.NextResult()方法推進下一個記錄:

using (var connection = new SqlConnection(yourConnectionString)) 
using (var command = new SqlCommand("yourStoredProcedure")) 
{ 
    connection.Open(); 
    using (var reader = command.ExecuteReader()) 
    { 
     while (reader.Read()) 
     { 
      // do something with first result set; 
     } 
     if (reader.NextResult()) 
     { 
      while (reader.Read()) 
      { 
       // do something with second result set; 
      } 
     } 
     else 
     { 
      return; 
     } 
     if (reader.NextResult()) 
     { 
      while (reader.Read()) 
      { 
       // do something with third result set; 
      } 
     } 
     else 
     { 
      return; 
     } 
    } 
} 
+0

我從來不知道你可以用'.ExecuteReader()'和'.NextResult()'''用'SqlDataReader'獲得多個結果集。感謝您的詳細解答! – JoeFletch

+2

的確,您可以,我通常更喜歡使用SqlDataReader,因爲只需存儲當前記錄即可降低內存使用量,並且不會打擾已讀過的內容以及尚未讀取的內容。然而,在[這個答案](http://stackoverflow.com/a/1083223/1048425)中有一個比較數據集和數據收集器的很好的比喻。 – GarethD

+0

感謝您分享SO鏈接!我過去使用過'SqlDataReader's,我認爲它需要很多代碼來維護。然後我嘗試了'DataTables',並認爲它們快得多。在將來編寫其他代碼時,我將不得不考慮備選方案!再次感謝分享! – JoeFletch

0

您可以檢查是否有醫生更多的記錄再次使用if (dr.NextResult())

,然後循環使用dr.read加時賽沒有

試試這個

string connStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; 
SqlConnection Con = new SqlConnection(connStr); 

try 
{ 
    string str1 = "select productid,productname from products;select VendorFName from vendor"; 
    SqlCommand com = new SqlCommand(str1, Con); 

    com.Connection.Open(); 

    SqlDataReader dr = com.ExecuteReader(); 

    DropDownList1.Items.Add("Select Product Id"); 
    DropDownList2.Items.Add("Select Vendor Name"); 

    while(dr.Read()) 
    { 
     DropDownList1.Items.Add(dr.GetValue(0).ToString()); 
    } 

    if (dr.NextResult()) 
    { 
     while (dr.Read()) 
     { 
      DropDownList2.Items.Add(dr.GetValue(0).ToString()); 
     } 
    } 
} 
catch (Exception ex) 
{ 
} 
finally 
{ 
    if (Con.State == ConnectionState.Open) 
    { 
     Con.Close(); 
    } 
} 
相關問題