2016-08-01 56 views
0

我試圖在我的C#應用​​程序中運行SELECT查詢,但即時消息實際上堅持實際上。我將如何從MySql數據庫中選擇一些東西?C#執行SQL查詢並返回或顯示響應

我當前的代碼如下所示:

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 Helpful 
{ 
    class database_connector 
    { 
     private MySqlConnection connection; 
     private string server; 
     private string database; 
     private string uid; 
     private string password; 

     // Constructor 
     public database_connector() 
     { 
      Initialize(); 
     } 

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

      connection = new MySqlConnection(connectionString); 
     } 

     //open connection to database 
     private bool OpenConnection() 
     { 
      try 
      { 
       connection.Open(); 
       return true; 
      } 
      catch (MySqlException ex) 
      { 
       switch (ex.Number) 
       { 
        case 0: 
         MessageBox.Show("Cannot connect to server. Contact administrator."); 
         break; 
        case 1045: 
         MessageBox.Show("Invalid username/password, please try again"); 
         break; 
       } 
       return false; 
      } 
     } 

     //Close connection 
     private bool CloseConnection() 
     { 
      try 
      { 
       connection.Close(); 
       return true; 
      } 
      catch (MySqlException ex) 
      { 
       MessageBox.Show(ex.Message); 
       return false; 
      } 
     } 

     //Select statement 
     public void user_check(string username, string password) 
     { 
      string query = "SELECT * FROM swear_tool WHERE username =" + username; 

      if (this.OpenConnection() == true) 
      { 
       MySqlCommand cmd = new MySqlCommand(query, connection); 
       MySqlDataReader dataReader = cmd.ExecuteReader(); 
       dataReader.Close(); 
       this.CloseConnection(); 
      } 
     } 
    } 
} 

但我怎麼可以看到,如果這一實際工作?因爲如果我嘗試在消息框中顯示結果,它將返回,無法轉換爲字符串。

if (this.OpenConnection() == true) 
{ 
    MySqlCommand cmd = new MySqlCommand(query, connection); 
    MySqlDataReader dataReader = cmd.ExecuteReader(); 
    MessageBox.Show(dataReader); 
    dataReader.Close(); 
    this.CloseConnection(); 
} 
+0

呀,你不能那樣做。這應該讓你朝着正確的方向前進:https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx – sgeddes

回答

1

您需要知道數據庫所需的字段。 使用類似SELECT Id, Name FROM swear_tool WHERE username =" + username的SQL和檢索Name字段的代碼。
指數0 =標識
指數1 =名稱< - GetString的(1)

if (dataReader.HasRows) { 
    while (dataReader.Read()) { 
     Console.WriteLine("{0}", dataReader.GetString(1)); 
    } 
} else { 
    Console.WriteLine("No rows."); 
}