2015-06-07 70 views
-1

我有一個表的數據庫,我有3選擇SQL語句。如何創建我的3選擇SQL語句的數據表

這些選擇項目具有不同的條件。我怎樣才能合併這3個SQL命令的答案?

我不想將它們按行逐行合併到數據表中。有沒有其他方法?

一些這樣的事..

  OleDbCommand cmd = new OleDbCommand("select top "+ cont0 +" * from (select * from db where tablenumber=0) order by ID ASC", mycon); 
     OleDbDataAdapter adapt=new OleDbDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     DataTable dttemp = new DataTable(); 
     adapt = new OleDbDataAdapter(cmd); 
     adapt.Fill(dt); 
     cmd = new OleDbCommand("select top "+ cont1 +" * from (select * from db where tablenumber=1) order by ID ASC", mycon); 
     adapt = new OleDbDataAdapter(cmd); 
     adapt.Fill(dttemp); 
     foreach (DataRow row in dttemp.Rows) 
     { 
      dt.Rows.Add(row.ItemArray); 
     } 
     if (cont2 != 0) 
     { 
      cmd = new OleDbCommand("select top " + cont2 + " * from (select * from db where tablenumber=2) order by ID ASC", mycon); 
      adapt = new OleDbDataAdapter(cmd); 
      dttemp = new DataTable(); 
      adapt.Fill(dttemp); 
      foreach (DataRow row in dttemp.Rows) 
      { 
       dt.Rows.Add(row.ItemArray); 
      } 
     } 
     if (cont3 != 0) 
     { 
      cmd = new OleDbCommand("select top " + cont3 + " * from (select * from db where tablenumber=3) order by ID ASC", mycon); 
      adapt = new OleDbDataAdapter(cmd); 
      dttemp = new DataTable(); 
      adapt.Fill(dttemp); 
      foreach (DataRow row in dttemp.Rows) 
      { 
       dt.Rows.Add(row.ItemArray); 
      } 
     } 
     if (cont4 != 0) 
     { 
      cmd = new OleDbCommand("select top " + cont4 + " * from (select * from db where tablenumber=4) order by ID ASC", mycon); 
      adapt = new OleDbDataAdapter(cmd); 
      dttemp = new DataTable(); 
      adapt.Fill(dttemp); 
      foreach (DataRow row in dttemp.Rows) 
      { 
       dt.Rows.Add(row.ItemArray); 
      } 
     } 
     if (cont5 != 0) 
     { 
      cmd = new OleDbCommand("select top " + cont5 + " * from (select * from db where tablenumber=5) order by ID ASC", mycon); 
      adapt = new OleDbDataAdapter(cmd); 
      dttemp = new DataTable(); 
      adapt.Fill(dttemp); 
      foreach (DataRow row in dttemp.Rows) 
      { 
       dt.Rows.Add(row.ItemArray); 
      } 
     } 
+2

你的問題還不清楚。你是說你沒有數據庫嗎?你想如何,何時何地創建它?它應該是什麼樣的數據庫?軟件應該能夠自動創建它,還是隻是一次?請編輯問題並添加相關信息。 – GolezTrol

+1

歡迎來到Stack Overflow。請參閱[如何提問](http://stackoverflow.com/help/how-to-ask)瞭解更多信息。謝謝。 –

+0

我有一個數據庫與一個表,我有3選擇sql語句那些選擇具有不同條件的項目,我怎樣才能合併這3個sql命令的答案?我不想將它們逐行地合併到數據表中。有沒有其他方法? –

回答

0

你可以簡單地將它們合併這樣的:

"select * from 
    (select *, row_number() over(partition by field order by id) as rn 
    from table1 where field in(0, 5, 9)) t 
where rn <= " + cont 

編輯:

string command = "select * from (select top "+ cont0 +" * from db where tablenumber=0 order by ID) t union all 
        select * from (select top "+ cont1 +" * from db where tablenumber=1 order by ID) t"; 

if (cont2 != 0) 
    comand += " union all select * from (select top " + cont2 + " * from db where tablenumber=2 order by ID) t"; 

if (cont3 != 0) 
    comand += " union all select * from (select top " + cont3 + " * from db where tablenumber=3 order by ID) t"; 

.... 

OleDbCommand cmd = new OleDbCommand(command, mycon); 
OleDbDataAdapter adapt=new OleDbDataAdapter(cmd); 
DataTable dt = new DataTable(); 
.... 
+0

但我的sql命令有點不同。我的sql命令是:select top「+ cont +」* from(select * from db where tablenumber =「+ mychoose +」)order by ID ASC –

+0

然後將這些信息添加到您的問題中。你認爲我們會猜測你有頂級? –

+0

@AfshinEzadi,參見編輯。在將來提供實際的陳述。 –