【问题标题】:Update sql database without blanks fields更新没有空白字段的sql数据库
【发布时间】:2019-10-22 21:11:45
【问题描述】:

如果我点击提交表单时某些字段为空白,我正在尝试为我的程序找出一种不更新 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
            }
        }

【问题讨论】:

  • 在更新数据库之前验证输入(控件的值)?
  • 如果数据库值不应该为空,那么数据库应该有一个约束。不要依赖前端来保持数据有效。
  • 首先从数据库中检索对象,然后检查表单输入是否为空,然后将它们替换为从数据库中获取的对象值。顺便说一句,我推荐使用实体框架。试一试..
  • 不要直接从 textbox.store 中选择一个局部变量然后使用IsNullorBlank进行检查
  • 参数名称应为“@Job_Number”等。

标签: c# sql winforms


【解决方案1】:

假设您想在一个或多个字段为空时更新某些字段,那么您可以这样做:

UPDATE Master_List
SET Date_Complete1 = ISNULL(NULLIF(@Date_Complete1,''),Date_Complete1),
    Pass_Fail = ISNULL(NULLIF(@Pass_Fail,''),Pass_Fail),
    CRC_Number = ISNULL(NULLIF(@CRC_Number,''),CRC_Number),
    QN_Number = ISNULL(NULLIF(@QN_Number,''),QN_Number),
    Notes = ISNULL(NULLIF(@Notes,''),Notes)
WHERE Job_Number = @Job_Number

如果您不想在任何字段为空的情况下更新任何字段,则只需在 if 语句中检查它们。

【讨论】:

  • 谢谢!这就是我一直在寻找的。​​span>
【解决方案2】:

在执行您的 sql 代码之前检查您的输入数据。

如果您想做的任何检查失败,只需退出函数即可,例如

If(string.IsNullOrEmpty(variable to check)) ...为用户返回或配置错误消息...

或者执行一些逻辑来向用户显示您不想要空白字段。

【讨论】:

    【解决方案3】:

    如果您要验证是否应该更改数据库字段属性以执行该任务。 如果你想用一些代码来做到这一点,你应该添加这样的东西:

       bool val = true;
          if (jobTxt.Text.Trim() == string.Empty) {
              val = false;
          }
    if(val==true){
        command.ExecuteNonQuery();
    }
    else{
    MessageBox.Show("Some field is empty")
    }
    

    如果要对每个文本框进行验证,则重复该句子。 我希望这对你有帮助。 您可以在文本框的 else 语句中说出哪个文本框是空的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      相关资源
      最近更新 更多