2012-07-05 46 views
15

我想填充包含兩個表的一對多關係的數據集。 我使用的DataReader實現這一目標:如何用多個表填充數據集?

public DataSet SelectOne(int id) 
    { 
     DataSet result = new DataSet(); 
     using (DbCommand command = Connection.CreateCommand()) 
     { 
      command.CommandText = "select * from table1"; 

      var param = ParametersBuilder.CreateByKey(command, "ID", id, null); 
      command.Parameters.Add(param); 

      Connection.Open(); 
      using (DbDataReader reader = command.ExecuteReader()) 
      { 
       result.MainTable.Load(reader); 
      } 
      Connection.Close(); 
     } 
     return result; 
    } 

但我有隻有一個表填滿。我如何實現我的目標 - 填寫兩張表格?

我想用DataReader代替DataAdapter,如果可能的話。

+3

爲什麼你會想到兩個表填寫?你的命令只包含一個返回單個表的'select'語句。 – 2012-07-05 13:53:54

+2

爲什麼不使用'SqlDataAdapter'及其'Fill(...)'方法而不是'DbCommand'? – bluevector 2012-07-05 13:54:09

+0

@Nikola Anusev - 我知道,所以我只是問任何建議 – 2012-07-05 13:55:24

回答

19

如果您發出帶有多個SELECT語句一條命令,你可以使用NextResult方法來DataReader的範圍內移動到下一個結果集:http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx

我展示它可能看起來波紋管:

public DataSet SelectOne(int id) 
{ 
    DataSet result = new DataSet(); 
    using (DbCommand command = Connection.CreateCommand()) 
    { 
     command.CommandText = @" 
select * from table1 
select * from table2 
     "; 

     var param = ParametersBuilder.CreateByKey(command, "ID", id, null); 
     command.Parameters.Add(param); 

     Connection.Open(); 
     using (DbDataReader reader = command.ExecuteReader()) 
     { 
      result.MainTable.Load(reader); 
      reader.NextResult(); 
      result.SecondTable.Load(reader); 
      // ... 
     } 
     Connection.Close(); 
    } 
    return result; 
} 
+7

你可以做'result.Load(reader)'。 Load方法將處理多個結果集。 – AMissico 2013-11-12 01:45:43

+0

@AMissico,true,但是哪個表填充哪個結果集不能用DataSet.Load明確聲明,如果以另一個順序定義表,它將會中斷。 你也必須陳述其他參數。 – smoothdeveloper 2015-12-19 19:41:43

+0

你讓我今天微笑。 – AMissico 2015-12-23 00:48:52

23

填充數據集無線可以通過向數據庫發送多個請求或以更快的方式完成多個表:可以通過單個請求將多個SELECT語句發送到數據庫服務器。這裏的問題是從查詢生成的表具有自動名稱Table和Table1。但是,生成的表名可以映射到應在DataSet中使用的名稱。

SqlDataAdapter adapter = new SqlDataAdapter(
     "SELECT * FROM Customers; SELECT * FROM Orders", connection); 
adapter.TableMappings.Add("Table", "Customer"); 
adapter.TableMappings.Add("Table1", "Order"); 

adapter.Fill(ds); 
+0

謝謝你的回覆,但根據我的任務 - 我需要使用DataReader代替DataAdapter:在我的問題中描述 – 2012-12-20 07:35:49

+3

加入TableMappings.Add不是neccassarry – 2014-10-22 10:06:42

7

這是一個老話題,但對於一些人來說可能是有用的:

 DataSet someDataSet = new DataSet(); 
     SqlDataAdapter adapt = new SqlDataAdapter(); 

     using(SqlConnection connection = new SqlConnection(ConnString)) 
     { 
      connection.Open(); 
      SqlCommand comm1 = new SqlCommand("SELECT * FROM whateverTable", connection); 
      SqlCommand comm2g = new SqlCommand("SELECT * FROM whateverTable WHERE condition = @0", connection); 
      commProcessing.Parameters.AddWithValue("@0", "value"); 
      someDataSet.Tables.Add("Table1"); 
      someDataSet.Tables.Add("Table2"); 

      adapt.SelectCommand = comm1; 
      adapt.Fill(someDataSet.Tables["Table1"]); 
      adapt.SelectCommand = comm2; 
      adapt.Fill(someDataSet.Tables["Table2"]); 
     } 
+0

如何處理這種情況,如果sql(store proc)返回多個表? – 2016-09-29 13:32:22

+1

而不是'adapt.Fill(someDataSet.Tables [「Table1」])''你會做'adapt.Fill(someDataSet)'。因爲你的存儲過程重用了表,所以只有當它確實返回TABLES而不是來自多個表的一組COLUMNS時。 – CularBytes 2016-09-29 14:11:33

+0

感謝您的迴應,將嘗試這件事情:-) – 2016-09-29 14:24:39

0
protected void Page_Load(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection("data source=.;uid=sa;pwd=123;database=shop"); 
    //SqlCommand cmd = new SqlCommand("select * from tblemployees", con); 
    //SqlCommand cmd1 = new SqlCommand("select * from tblproducts", con); 
    //SqlDataAdapter da = new SqlDataAdapter(); 

    //DataSet ds = new DataSet(); 
    //ds.Tables.Add("emp"); 
    //ds.Tables.Add("products"); 
    //da.SelectCommand = cmd; 
    //da.Fill(ds.Tables["emp"]); 
    //da.SelectCommand = cmd1; 

    //da.Fill(ds.Tables["products"]); 
    SqlDataAdapter da = new SqlDataAdapter("select * from tblemployees", con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "em"); 
    da = new SqlDataAdapter("select * from tblproducts", con); 
    da.Fill(ds, "prod"); 

    GridView1.DataSource = ds.Tables["em"]; 
    GridView1.DataBind(); 
    GridView2.DataSource = ds.Tables["prod"]; 
    GridView2.DataBind(); 
} 
+0

請添加一些解釋。 – gofr1 2016-09-28 15:07:08