C# 在MS Access数据库的双字段中插入null(不带参数)

C# 在MS Access数据库的双字段中插入null(不带参数),c#,sql,ms-access,sql-insert,bulkinsert,C#,Sql,Ms Access,Sql Insert,Bulkinsert,我正试图在MS Access数据库的双字段中插入空值,但我不能。我在下面分享我的代码,有人能帮我吗 for (int i = 0; i <= RemovedDuplicateDt.Rows.Count - 1; i++) { checkBool = Convert.ToBoolean(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(2).ToString());

我正试图在MS Access数据库的双字段中插入空值,但我不能。我在下面分享我的代码,有人能帮我吗

 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();
                }
因为使用速度太慢了

我的数据库设计就是这样


我在等待你的答复,谢谢

您可以通过更改这一行来这样使用

+ "'" + 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查询可能会导致问题,这是一个大问题。假设在datatable单元格值中,“
”;DROP TABLE Data;——”

可以通过更改这一行这样使用

+ "'" + 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查询可能会导致问题,这是一个大问题。假设在datatable单元格中的值“
””;DROP TABLE Data;--”“

RemovedDuplicateDt
中,双单元格包含
0
或`null`?在RemovedDuplicateDt中,所有单元格都是字符串格式的&此单元格在
RemovedDuplicateDt
中为null,而
双单元格
包含
0
或`null`?在RemovedDuplicateDt中,所有单元格都是字符串格式的&此单元格为空。”你能分享那个错误代码吗?看不到代码的完整部分我尝试了我的答案,使用
parantisis
正确地工作
isnullorEmpty
。并更改我显示的行方式,不要在开始时使用“””,但不明白为什么代码不会抛出“数据类型错误匹配错误”?因为db列类型是double,但您将“null”作为参数发送。这是文本null,sql接受它作为任何类型的null值。您可以共享该错误行代码吗?看不到代码的完整部分我尝试了我的答案,使用
parantisis
正确地工作
isnullorEmpty
。并更改我显示的行方式,不要在开始时使用“””,但不明白为什么代码不会抛出“数据类型错误匹配错误”?因为db列类型是double,但您将“null”作为参数发送,这是文本null,所以sql接受它作为任何类型的null值