如何将 DBNull.Value 与 datagridview 单元格一起使用?

How can I use DBNull.Value with datagridview cells?

我有这种方法可以将来自 Datagridview 的数据插入到我的数据库中的 table 中,如果我的 Datagridview 中的所有单元格都不是 Null但是,如果某些单元格为空或为空,则会抛出错误:

The parameterized query expects the parameter , which was not supplied

事实是一些单元格可以为空并且数据库会接受,我需要的所需单元格已经由用户强制填写,因此可选单元格可以为空。

我的方法:

private void InsertAll()
{
    using (var cn = new SqlConnection(Cn.ConnectionString)) // Using block around the connection
    using (var cmd = new SqlCommand("", cn))
    {

        cmd.CommandText = @" INSERT INTO Transactions ( DocNum, Code, QtyIn, QtyOut, BalanceAfter, Remarks, Unit )
                     Values (@DocNum, @Code, @QtyIn, @QtyOut, @BalanceAfter, @Remarks,@Unit);";

        cmd.Parameters.Add("@DocNum", SqlDbType.NVarChar, 50);
        cmd.Parameters.Add("@Code", SqlDbType.NVarChar, 50);
        cmd.Parameters.Add("@QtyIn", SqlDbType.Int).Value = 0;
        cmd.Parameters.Add("@QtyOut", SqlDbType.Int).Value = 0;
        cmd.Parameters.Add("@BalanceAfter", SqlDbType.Int);
        cmd.Parameters.Add("@Remarks", SqlDbType.NVarChar, 50);
        cmd.Parameters.Add("@Unit", SqlDbType.NVarChar, 10);

        var qtyKey = (txtTransType.Text == "Release") ? "@QtyOut" : "@QtyIn";

        for (int i = 0; i < DGV1.Rows.Count; i++)
        {
            // These are 3 key fields which can't be null and I force the user to fill .
            // So no need to check if null cause it always will have some value .
            
            cmd.Parameters["@DocNum"].Value = txtDocNum.Text;
            cmd.Parameters["@Code"].Value = DGV1.Rows[i].Cells["Code"].Value;
            cmd.Parameters[qtyKey].Value = DGV1.Rows[i].Cells["Qty"].Value;

            // The rest is the optional cells that the db accepts to be null

            // Then I need to check if they are null and pass the DBNull.Value .            

            // Try DBValue.Null
            if (String.IsNullOrEmpty(DGV1.CurrentRow.Cells["BalanceAfter"].Value.ToString()))
            {
                cmd.Parameters["@BalanceAfter"].Value = DBNull.Value;
            }
            else
            {
                cmd.Parameters["@BalanceAfter"].Value = DGV1.Rows[i].Cells["BalanceAfter"].Value;
            }
            // Another Try

            if (String.IsNullOrEmpty(Remarks.ToString()))
            {
                cmd.Parameters["@Remarks"].Value = DBNull.Value;
            }
            else
            {
                cmd.Parameters["@Remarks"].Value = DGV1.Rows[i].Cells["Remarks"].Value;
            }
            if (String.IsNullOrEmpty(Unit.ToString()))
            {
                cmd.Parameters["@Unit"].Value = DBNull.Value;
            }
            else
            {
                cmd.Parameters["@Unit"].Value = DGV1.Rows[i].Cells["Unit"].Value;
            }

            cn.Open();
            cmd.ExecuteNonQuery();

        }
    }
}

这部分:

  if   (String.IsNullOrEmpty(DGV1.CurrentRow.Cells["BalanceAfter"].Value.ToString()))
                    {
                        cmd.Parameters["@BalanceAfter"].Value = DBNull.Value;
                    }
                    else
                    {
                        cmd.Parameters["@BalanceAfter"].Value = DGV1.Rows[i].Cells["BalanceAfter"].Value;
                    }

抛出错误:

Object reference not set to an instance of object

我也试过这个:

 if (String.IsNullOrEmpty(Remarks.ToString()))
                {
                    cmd.Parameters["@Remarks"].Value = DBNull.Value;
                }
                else
                {
                    cmd.Parameters["@Remarks"].Value = DGV1.Rows[i].Cells["Remarks"].Value;
                }

但是导致原来的错误就好像它根本不存在一样。 感谢您的帮助,谢谢

伙计们,这是有效的,不使用字符串方法的简单空检查

  if (DGV1.CurrentRow.Cells["BalanceAfter"].Value == null)
                    {
                        cmd.Parameters["@BalanceAfter"].Value = DBNull.Value;
                    }
                    else
                    {
                        cmd.Parameters["@BalanceAfter"].Value = DGV1.Rows[i].Cells["BalanceAfter"].Value;
                    }

谢谢大家