2013-05-10 26 views
0

我有一個應用程序,我試圖創建,但是我卡住了。在WinForms應用程序中呈現來自MySQL的數據

我想在我的代碼中訪問該網站的MySQL查詢。

現在我只是試圖測試它,以確保它的工作,但我沒有運氣。

我想我只是俯視簡單的東西,但我不確定。任何幫助?

class DBConnect 
{ 
    private MySqlConnection connection; 
    private string server; 
    private string database; 
    private string uid; 
    private string password; 

    public List<string>[] list = new List<string>[1]; 

    public DBConnect() 
    { 
     Initialize(); 
    } 

    private void Initialize() 
    { 
     server = "localhost"; 
     database = "XXX"; 
     uid = "XXX"; 
     password = "XXX"; 
     string connectionString; 
     connectionString = "SERVER=" + server + ";" + "DATABASE=" + database 
      + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; 

     connection = new MySqlConnection(connectionString); 
    } 

    //this will open the connection to MySql 
    private bool OpenConnection() 
    { 
     try 
     { 
      connection.Open(); 
      return true; 
     } 
     //if there is a problem connecting, display one of the following connections 
     catch (MySqlException ex) 
     { 
      switch (ex.Number) 
      { 
       case 0: 
        MessageBox.Show("Cannot connect to server. Contact Admin"); 
        break; 
       case 1045: 
        MessageBox.Show("Invalid username/password, please try again"); 
        break; 
      } 
      return false; 
     } 
    } 

    //if the connection fails to connect to MySql server, the client will close the connection. 
    private bool CloseConnection() 
    { 
     try 
     { 
      connection.Close(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      MessageBox.Show(ex.Message); 
      return false; 
     } 
    } 

    //Select statement 
    public List<string>[] Select() 
    { 
     int firstNumber = 2058; 
     **string query = "USE `db_order` SELECT `order_id` FROM `order_address` WHERE order_id=2058";** 

     //create a list to store the results 
     list[0] = new List<string>(0); 

     //Open's connection 
     if (this.OpenConnection() == true) 
     { 
      //creates a command from the query. 
      MySqlCommand cmd = new MySqlCommand(query, connection); 
      //creates a data reader, and executes the command. 
      MySqlDataReader dataReader = cmd.ExecuteReader(); 

      while (dataReader.Read()) 
      { 
       list[0].Add(dataReader["order_id"] + ""); 
      } 

      //close Data Reader 
      dataReader.Close(); 

      this.CloseConnection(); 

      //returns list to be displayed 
      return list; 
     } 
     else 
     { 
      return list; 
     } 
    } 
} 

這裏是第二類(我擺脫的東西,是不是問題的一部分):

public partial class Form1 
{ 
    private void InitializeComponent() 
    { 
     // CustName 
     // 
     this.CustName.AutoSize = true; 
     this.CustName.Location = new System.Drawing.Point(200, 30); 
     this.CustName.Name = "CustName"; 
     this.CustName.Size = new System.Drawing.Size(137, 13); 
     this.CustName.TabIndex = 0; 
     this.CustName.Text = "Customer\'s name goes here" + new DBConnect().list[0]; 


    } 

    #endregion 
} 
+1

是否返回任何異常消息? – Edper 2013-05-10 03:35:37

回答

0

更新

您從添加到列表中DataReader這樣的

list[0].Add(dataReader["order_id"] + ""); 

所以,這意味着你h ave a multi-dimensional(2維準確)列表數組。因此要訪問您必須做的Select()[0][0],請檢查以下內容:

DBConnect() DBCon = new DBConnect(); 

this.Custname.Text = DBCon.Select()[0].Count>0 ? "Customer\'s name goes here" +DBCon.Select()[0][0].ToString() : "No Customer to show"; 
+0

嗨, 該網站有更多的一個數據庫,所以我需要使用聲明,或以其他方式訪問它。 – 2013-05-10 04:31:04

+0

好的。我明白。 – Edper 2013-05-10 04:40:31

+0

@AlexMoreno嘗試我的更新。 – Edper 2013-05-10 05:10:25

相關問題