2013-04-16 85 views
1

我有兩個表,table1table2。我想從table2table1column1檢索column1。然後結合這兩個結果並作爲單獨的列在網格視圖中顯示。這裏是我的代碼:合併來自mysql中兩個數據集的結果

string query4 = "select tablename from table1 where tid='1'"; 
MySqlCommand command4 = new MySqlCommand(query4, connection); 

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

GridView1.DataSource = ds; 
GridView1.DataBind(); 
string query5 = "select fname from table2 where id='1'"; 
MysqlCommand command5 = new MySqlCommand(query5, connection); 

DataSet ds2 = new DataSet(); 
adapter.SelectCommand = command5; 
adapter.fill(ds2); 

Gridview1.DataSource = ds; 

這給了我只有table1價值,但我想在單個網格視圖中顯示來自table1table2兩列。

+0

你的意思行或列一個DataSet?以下只有UNION響應與將它們作爲2行和1列返回有關。我的回覆(JOIN)涉及讓他們返回爲1行,2列。我不確定凱文的答案會給你什麼 –

回答

0

你可以做到這一點有兩種方法中的所有數據。第一個是從MySQL查詢像這樣:

string query6 = "select tablename AS value from table1 where tid='1' UNION select fname from table2 as value where id='1'"; 

或結合您的DataSets使用DataSet.Merge();