2011-04-30 49 views
1

需要幫助, 我需要將標題放入名爲cntpaginaBX和prmopaginaBX的組合框中。 與一個它的作品,但是當我嘗試兩個..它失敗。 我想知道的是「MySqlCommand」。 我如何解決它,因爲它不採取內容,促銷,sqlconn togheter。MySqlCommand:如何將標題放入組合框

 MySqlConnection sqlConn = new MySqlConnection("Database=joshua; 
     Data Source=localhost; User Id='root'; Password=''"); 
     string content = "SELECT title FROM content"; 
     string promo = "SELECT title FROM promo"; 
     MySqlCommand myCommando = new MySqlCommand(content, promo, sqlConn); <-----here 
     MySqlDataReader sqlReader; 
     object cont; 
     object prom; 

     try 
     { 
      sqlConn.Open(); 
      sqlReader = myCommando.ExecuteReader(); 
      while (sqlReader.Read()) 
      { 
       cont = sqlReader.GetValue(0).ToString(); 
       cntpaginaBX.Items.Add(cont); 
       prom = sqlReader.GetValue(0).ToString(); 
       prmopaginaBX.Items.Add(prom); 
      } 

      sqlReader.Close(); 
     } 
     catch (Exception x) 
     { 
      MessageBox.Show(x.Message, "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     finally 
     { 
      sqlConn.Close(); 
     } 

回答

0

MySqlCommand用於運行一個單個查詢。你不能簡單地一起運行兩個查詢。每個查詢需要是完全獨立的MySqlCommand

string content = "SELECT title FROM content"; 
MySqlCommand contentCommand = new MySqlCommand(content, sqlConn); 
try 
{ 
    sqlConn.Open(); 
    sqlReader = contentCommand.ExecuteReader(); 
    while (sqlReader.Read()) 
    { 
     cont = sqlReader.GetValue(0).ToString(); 
     cntpaginaBX.Items.Add(cont); 
    } 

    sqlReader.Close(); 
} 

然後:

string promo = "SELECT title FROM promo"; 
MySqlCommand promoCommand = new MySqlCommand(promo, sqlConn); 
try 
{ 
    sqlConn.Open(); 
    sqlReader = promoCommand.ExecuteReader(); 
    while (sqlReader.Read()) 
    { 
      prom = sqlReader.GetValue(0).ToString(); 
      prmopaginaBX.Items.Add(prom); 
    } 

    sqlReader.Close(); 
} 
+0

感謝您的幫助 – 2011-04-30 13:33:44

+0

關注的問題是在prmopaginaBX從內容的標題都出現了 – 2011-04-30 13:52:52

+0

好吧,我得到它,發現了一些沒有刪除的行 – 2011-04-30 13:57:22