2015-12-01 48 views
3

剛剛開始編碼C#,我是一個絕對的初學者。話雖如此,我有一個關於使用MySqlConnector和MySQL查詢的問題。C#visual studio和多個mySQL查詢的

我目前有一個查詢,並用它填充列表框中的結果。但我想要做的是將更多的查詢添加到閱讀器中,並將其他查詢的結果放在組合框中。那麼,我該如何做到這一點?我搜索谷歌並找不到答案。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 

namespace Badminton_stand 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 


     private void timer1_Tick(object sender, EventArgs e) 
     { 
      labelCurrentTime.Text = DateTime.Now.ToString(); 
     } 



     private void Form1_Load(object sender, EventArgs e) 
     /* { 
       string MyConString = "Server=localhost;Port=3307;Database=database1;UID=root;Password=toor"; 
       MySqlConnection connection = new MySqlConnection(MyConString); 
       MySqlCommand command = connection.CreateCommand(); 
       MySqlDataReader Reader; 
       command.CommandText = @"select Club from teams; 
        select team from teams"; 
       connection.Open(); 
       Reader = command.ExecuteReader(); 
       while (Reader.Read()) 
       { 
        string thisrow = ""; 
        for (int i = 0; i < Reader.FieldCount; i++) 
         thisrow += Reader.GetValue(i).ToString() + " "; 
        clubSelectorBox1.Items.Add(thisrow); 
       } 
       while (Reader.NextResult()) ; 
       { 
        string thisrow = ""; 
        for (int i = 0; i < Reader.FieldCount; i++) 
         thisrow += Reader.GetValue(i).ToString() + " "; 
        cbTeam1.Items.Add(thisrow); 
       } 
       connection.Close(); 
      } */ 
     { 
      string cmdText = @"SELECT Club from teams; 
        SELECT team from teams"; 
      using (MySqlConnection cnn = new MySqlConnection("Server=localhost;Port=3307;Database=badminton;UID=root;Password=usbw")) 
      using (MySqlCommand cmd = new MySqlCommand(cmdText, cnn)) 
      { 
       cnn.Open(); 
       using (MySqlDataReader reader = cmd.ExecuteReader()) 
       { 
        do 
        { 
         while (reader.Read()) 
         { 
          string thisrow = ""; 
          for (int i = 0; i < reader.FieldCount; i++) 
           thisrow += reader.GetValue(i).ToString() + " "; 
          clubSelectorBox1.Items.Add(thisrow); 
         } 

        } 
        while (reader.NextResult()); 
        { 

        } 
       } 
      } 

     } 
    } 
} 

在此先感謝!

回答

0

爲每個附加查詢初始化一個新的MySqlCommand。

MySqlCommand command2 = connection.CreateCommand(); 
command2.CommandText = ""; // here the new query 
3

你可以把多個SELECT語句中的查詢文本和循環利用上了MySqlDataReader的Ne​​xtResult方法的結果。

這是一個例子,如何做到這一點,當然,你要適應你的數據和控制

string cmdText = @"SELECT * from TABLE_A; 
        SELECT * from TABLE_B"; 
using(MySqlConnection cnn = new MySqlConnection(......)) 
using(MySqlCommand cmd = new MySqlCommand(cmdText, cnn)) 
{ 
    int curTable = 0; 
    cnn.Open(); 
    using(MySqlDataReader reader = cmd.ExecuteReader()) 
    { 
     do 
     { 
      while (reader.Read()) 
      { 
       if(curTable == 0) 
        Console.WriteLine("Update first list based on the first table"); 
       else if(curTable == 1) 
        Console.WriteLine("Update second list based on second table"); 
      } 
      Console.WriteLine("Go to next result"); 
      curTable++; 
     } 
     while(reader.NextResult()); 
    } 
} 
+0

這就是我一直在尋找的!謝謝史蒂夫 – Dennis1679

+0

試圖讓這與我想要的工作,但它不工作。當我循環閱讀器。閱讀,它也從第二個查詢字符串的結果,並將其放置在我的列表框。你能幫我嗎?看到我的第一篇文章更新代碼。 – Dennis1679

+0

現在有點忙,你應該保留當前循環的計數器並更新正確的列表 – Steve

1

使用功能。

private void Form1_Load(object sender, EventArgs e) 
     { 
      LoadCombo(clubSelectorbox1, "select club from teams"); 
      //now just change the control and the query as needed 
      LoadCombo(clubSelectorbox2, "select club from teams"); 
     } 

private function LoadCombo(DropDownControl myCombo, string query){ 
string MyConString = "Server=localhost;Port=3307;Database=database1;UID=root;Password=toor"; 
      MySqlConnection connection = new MySqlConnection(MyConString); 
      MySqlCommand command = connection.CreateCommand(); 
      MySqlDataReader Reader; 
      command.CommandText = query; // use query here 
      connection.Open(); 
      Reader = command.ExecuteReader(); 
      while (Reader.Read()) 
      { 
       string thisrow = ""; 
       for (int i = 0; i < Reader.FieldCount; i++) 
        thisrow += Reader.GetValue(i).ToString() + " "; 

       myCombo.Items.Add(thisrow); //use myControl here 
      } 
      connection.Close(); 
     }