private void Update_Record_Click(object sender, EventArgs e)
{
ConnectionClass.OpenConnection();
if (textBox4.Text == "" && textBox2.Text == "")
{
MessageBox.Show("No value entred for update.");
}
else if (textBox4.Text != "" && textBox2.Text != "")
{
SqlCommand cmd = new SqlCommand("update medicinerecord set quantity='" + textBox2.Text + "' where productid='"+comboBox1.Text+"'", ConnectionClass.OpenConnection());
cmd.ExecuteNonQuery();
cmd = new SqlCommand("update myrecord set price='" + textBox4.Text + "' where productid='" + comboBox1.Text + "'", ConnectionClass.OpenConnection());
cmd.ExecuteNonQuery();
ConnectionClass.CloseConnection();
}
else if (textBox2.Text != "")
{
SqlCommand cmd = new SqlCommand("update myrecord set quantity='" + textBox2.Text + "' where productid='" + comboBox1.Text + "'", ConnectionClass.OpenConnection());
cmd.ExecuteNonQuery();
ConnectionClass.CloseConnection();
}
else if (textBox4.Text != "")
{
SqlCommand cmd = new SqlCommand("update myrecord set price='" + textBox4.Text + "' where productid='" + comboBox1.Text + "'", ConnectionClass.OpenConnection());
cmd.ExecuteNonQuery();
ConnectionClass.CloseConnection();
}
}
它工作正常,但我想縮短它以便更容易理解。我如何重構它?我該如何重構這種方法?
使用您的編輯器的縮小功能,使代碼小..... – 2011-05-26 12:18:00
我編輯的問題,使其更清晰。 – 2011-05-26 12:20:41
如果您希望提高其他人對此的理解,也可以考慮爲您的控件提供有意義的ID。 – schummbo 2011-05-26 12:20:42