2013-02-25 95 views
-2

我想在表單加載的數據庫中獲取多個數據。但是我發現代碼很長並且重複。有人可以讓這個代碼縮短一點嗎?在表單上加載數據庫的多個數據加載

這是我的代碼

private void Form1_Load(object sender, EventArgs e) 
    { 
     string a = label1.Text; 
     string connString = "Server=Localhost;Database=this;Uid=root;password=root"; 
     using (var connection = new MySqlConnection(connString)) 
     { 
      connection.Open(); 
      using (var command = connection.CreateCommand()) 
      { 
       command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)"); 
       command.Parameters.AddWithValue("?room", a); 
       command.ExecuteNonQuery(); 

       MySqlDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
        button1.Text = reader["Room_name"].ToString(); 
       } 
      } 
     } 
     string b = label2.Text; 
     string connString2 = "Server=Localhost;Database=this;Uid=root;password=root"; 
     using (var connection = new MySqlConnection(connString2)) 
     { 
      connection.Open(); 
      using (var command = connection.CreateCommand()) 
      { 
       command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)"); 
       command.Parameters.AddWithValue("?room", b); 
       command.ExecuteNonQuery(); 

       MySqlDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
        button2.Text = reader["Room_name"].ToString(); 
       } 
      } 

     } 
    } 
+0

這可能更適合CodeReview ... codereview.stackexchange.com,但是它可以做得更乾淨一點。例如......你真的需要有兩個字段爲同一個連接字符串? – Arran 2013-02-25 15:32:08

回答

1

首先要連接到同一個數據庫中所有的兩倍。 而你用同樣的方法來做它(很明顯,你是否完全決定是否只連接一次)。

其次你可以將實際的選擇執行包裝在一個單獨的方法中。 這個單獨的方法應該從調用者那裏接收它的連接(因爲你可以在這種特殊情況下只連接到數據庫,但是可以根據需要進行多次選擇)。

三,不要使用ExecuteReader,請使用ExecuteScalar。

更多:你不應該在FormLoad上做太多事情(UI會凍結)。 爲什麼你要根據標籤的值查詢數據庫? 僅僅是您寫的一個樣本能夠真正快速地向我們展示您的意思,還是實際的業務邏輯?

private string GetRoomName_BasedOn_RoomNumber(string roomNumber, MyConnection connection) { 
    using (var command = connection.CreateCommand()) 
    { 
     command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)"); 
     command.Parameters.AddWithValue("?room", roomNumber); 
     //command.ExecuteNonQuery(); 

     object response = command.ExecuteScalar(); 

     return response as string; // consider <null> as a "No such Room Number" signal 
    } 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    string a = label1.Text; 
    string b = label2.Text; 

    string connString = "Server=Localhost;Database=this;Uid=root;password=root"; 
    using (var connection = new MySqlConnection(connString)) 
    { 
     connection.Open(); 
     button1.Text = this.GetRoomName_BasedOn_RoomNumber(a, connection); 
     button2.Text = this.GetRoomName_BasedOn_RoomNumber(b, connection); 
    } 
} 
+0

關閉但你的代碼中很少有錯誤,你在哪裏聲明瞭命令類型?並且您沒有將連接分配給命令對象。 – 2013-02-25 15:43:22

+0

方法GetRoomName_BasedOn_RoomNumber沒有重載'需要1個參數.. – 2013-02-25 15:46:42

+0

好吧..對不起'布特那。被帶走了。請檢查編輯 – 2013-02-25 15:47:28

0

例如,你可以重構你的代碼和下面的代碼提取到一個方法:

private void DoSomething(MySqlConnection connection) 
{    
using (var command = connection.CreateCommand()) 
     { 
      command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)"); 
      command.Parameters.AddWithValue("?room", b); 
      command.ExecuteNonQuery(); 

      MySqlDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       button2.Text = reader["Room_name"].ToString(); 
      } 
     } 

    }