如果我点击提交表单时某些字段为空白,我正在尝试为我的程序找出一种不更新 sql 数据库的方法。现在,当我将其提交到 sql 数据库时,如果字段为空白,它会将其更新为空白。有没有办法让我的代码不这样做?
谢谢
//field names in the table
string update = @"UPDATE Master_List
SET Date_Complete1 = @Date_Complete1, Pass_Fail = @Pass_Fail, CRC_Number = @CRC_Number, QN_Number = @QN_Number, Notes = @Notes WHERE Job_Number = @Job_Number"; //parameter names
using (SqlConnection conn = new SqlConnection(connString)) //using allows disposing of low level resources
{
try
{
conn.Open();//open new connection
command = new SqlCommand(update, conn); // create the new sql command object
// Read value from form and save to the table
command.Parameters.AddWithValue(@"Job_Number", jobTxt.Text);
command.Parameters.AddWithValue(@"Pass_Fail", comboBox1.Text);
command.Parameters.AddWithValue(@"Date_Complete1", opBox1.Text);
command.Parameters.AddWithValue(@"CRC_Number", crcTxt.Text);
command.Parameters.AddWithValue(@"QN_Number", qnTxt.Text);
command.Parameters.AddWithValue(@"Notes", notesTxt.Text);
command.ExecuteNonQuery(); // Push form into the table
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); // If there is something wrong, show the user message
}
}