2013-08-24 20 views
-1

我有多個數據database所以我需要他們在不同的textBoxes。 這裏是我的代碼如何搜索數據庫中的數據並將其添加到C#中的文本框?

private void Search_button1_Click(object sender, EventArgs e) 
{ 
    string query = string.Empty; 
    if (ID_textBox1.Text.Trim().Length > 0) 
    { 
     try 
     { 
      query = "SELECT ProductName,ProductDescription,SellPrice FROM Table2 WHERE ProductID='" + ID_textBox1.Text + "'"; 
      SqlConnection Conn = CreateConnection.create_connection(); 
      SqlCommand cd = new SqlCommand(query, Conn); 
      SqlDataReader reader = cd.ExecuteReader(); 

      while (reader.Read()) 
      { 
       Name_textBox2.Text = reader["ProductName"].ToString(); 
       Description_textBox3.Text = reader["ProductDescription"].ToString(); 
       Unit_Price_textBox5.Text = reader["SellPrice"].ToString(); 
      } 
      reader.Close(); 
      Name_textBox2.Text = Name_textBox2.Text; 
      Description_textBox3.Text = Description_textBox3.Text; 
      QTY_textBox4.Text = 1.ToString(); 
      Unit_Price_textBox5.Text = Unit_Price_textBox5.Text; 
      Price_textBox6.Text = (decimal.Parse(QTY_textBox4.Text) * decimal.Parse(Unit_Price_textBox5.Text)).ToString(); 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 
+2

那麼問題是什麼? –

+1

爲什麼你將'TextBox'中的值分配給自己?另外,學會使用參數化查詢來避免SQL注入,並且您希望查詢中只有一行結果?如果你有多行,只有最後一行的數據會在不同的文本框中出現。 – Tim

+0

什麼是'1.ToString()'應該產生? '1'不是有效的變量名稱。如果你想爲該文本框賦值1,只需使用'QTY_textBox4.Text =「1」;'。 – Tim

回答

1

你不說出你的問題是什麼,但有幾件事情我建議這樣做。

  1. 使用參數化查詢。這將防止SQL注入攻擊。

  2. 使用using聲明來確保正確處理事情。

  3. 這樣的行:Name_textBox2.Text = Name_textBox2.Text;是不必要的 - 您只需將值分配給自己。

  4. 1.ToString()沒有任何意義。 1不是有效的變量名稱。如果你想將1的值賦給文本框,只需使用QTY_textBox4.Text = "1";即可。

我會重寫你的代碼看起來像這樣:

if (ID_textBox1.Text.Trim().Length > 0) 
{ 
    try 
    { 
     query = "SELECT ProductName,ProductDescription,SellPrice FROM Table2 WHERE [email protected]"; 

     using (SqlConnection Conn = CreateConnection.create_connection()) 
     { 

      // NOTE: If CreateConnection.create_connection() does not return 
      // an opened connection, you will need to open it like this: 
      // Conn.Open(); 
      SqlCommand cd = new SqlCommand(query, Conn); 
      cd.Parameters.AddWithValue("@ProductID", ID_textBox1.Text); 

      using (SqlDataReader reader = cd.ExecuteReader()) 
      { 

       while (reader.Read()) 
       { 
        Name_textBox2.Text = reader["ProductName"].ToString(); 
        Description_textBox3.Text = reader["ProductDescription"].ToString(); 
        Unit_Price_textBox5.Text = reader["SellPrice"].ToString(); 
       } 
      } 
     } 

     decimal quantity; 
     decimal unitPrice; 

     QTY_textBox4.Text = "1"; 

     decimal.TryParse(QTY_textBox4.Text, out quantity); 
     decimal.TryParse(Unit_Price_textBox5.Text, unitPrice); 
     Price_textBox6.Text = (quantity * unitPrice).ToString(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

上面的代碼使用參數化查詢 - "SELECT ProductName,ProductDescription,SellPrice FROM Table2 WHERE [email protected]"@ProductID是參數的佔位符。

該參數由cd.Parameters.AddWithValue("@ProductID", ID_textBox1.Text);行填充。

using語句用於SqlConnectionSqlDataReader,並且將確保對象正確關閉和處置,即使發生異常。

我刪除了無用的行,其中TextBox的位置被賦予其當前值,就像在上面的循環中完成的那樣。

最後,我建議使用TryParse,因爲如果解析不成功,則不會拋出錯誤。實際上,如果解析不成功(TryParse返回布爾值),則可以使用TryParse顯示消息。

基於查詢,我猜你只希望得到一行數據,但是如果你得到多行數據,只有最後一行將是TextBoxes中的最終值。

沒有更多的信息,我們可以告訴你更多的不僅僅是這些。我希望它有幫助。

+0

沒有錯誤,但是當我單擊數據庫中的搜索按鈕值時無法進入文本框。是什麼原因 ????? – mahfuz110244

+0

@ user2713054 - 可能有多種原因。查詢是否返回數據?你是否已經在調試器中檢查代碼以確保數據實際上回來了? – Tim

相關問題