【问题标题】:Insert null into double field in MS Access Database(not with parameters)将 null 插入 MS Access 数据库中的双字段(不带参数)
【发布时间】:2017-02-02 07:42:47
【问题描述】:

我正在尝试将空值插入 MS Access 数据库中的 Double 字段,但我不能。我在下面分享我的代码,有人可以帮助我吗?

 for (int i = 0; i <= RemovedDuplicateDt.Rows.Count - 1; i++)
                {


                    checkBool = Convert.ToBoolean(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(2).ToString());

                    if (checkBool)
                    {
                        k = -1;
                    }
                    else
                    {
                        k = 0;
                    }

                    if (string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()))
                    {
                        scaling = DBNull.Value;
                    }
                    else
                    {
                        scaling = Convert.ToDouble(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString());
                    }

                    cmd.CommandText = "INSERT INTO " + "Data VALUES ('" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(0).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(1).ToString() + "' ,"
                        + "'" + k + "' ,"
                        + "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3)) + "' ,"
                        + "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(4).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(4)) + "' ,"
                        + "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(5).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(5)) + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(6).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(7).ToString() + "' ,"
                        + "'" + Convert.ToInt16(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(8).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(8)) + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(9).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(10).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(11).ToString() + "' ,"
                        + "'" + Convert.ToInt32(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(12).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(12)) + "' ,"
                        + "'" + Convert.ToInt32(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(13).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(13)) + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(14).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(15).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(16).ToString() + "' ,"
                        + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(17).ToString() + "' ,"
                        + "'" + Convert.ToInt32(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(18).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(18)) + "' )";

                    cmd.ExecuteNonQuery();
                }

此代码有效,但我不想在数据库中看到“0”,我想看到 null(空白)。

当我尝试 Convert.ToDouble(null) 时会引发错误,当我尝试提供 DBNull.Value 时会引发“数据库不匹配条件”错误。

我所需要的只是如何使用此代码将 null 插入数据库。

不想用;

cmd.Parameters.AddWithValue("Scaling", Double.TryParse(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString(), out result) ? (object)result : DBNull.Value);,

因为使用太慢了,

我的数据库设计就是这样;

我正在等待您的回复,谢谢!

【问题讨论】:

  • RemovedDuplicateDt 中,double cell 包含0 或`null`?
  • 在 RemovedDuplicateDt 中的所有单元格都采用字符串格式,并且该单元格为空

标签: c# sql ms-access sql-insert bulkinsert


【解决方案1】:

你可以通过改变这一行来像这样使用

+ "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3)) + "' ,"

到这里

+ (string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()) ? "null" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3)) + " ,"

注意: 但是如果使用命令参数会更好,连接SQL查询可能会导致SQL injection,这是个大问题。认为在您的数据表单元格值'"); DROP TABLE Data ; -- "'

【讨论】:

  • 你能分享那个错误行代码吗?无法看到正确的部分代码
  • 我编辑了我的答案,使用paranthesis 正确工作isnullorEmpty。并改变我的显示方式,不要在开始时使用"'"
  • 但是不明白为什么你的代码没有抛出“数据类型不匹配错误”?因为 db 列类型是双精度,但您发送“null”作为参数
  • 这是字面意义上的 null,sql 接受它作为任何类型的 NULL 值
猜你喜欢
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2013-12-14
  • 1970-01-01
相关资源
最近更新 更多