将空值插入 MS Access 数据库中的双字段(不带参数)
Insert null into double field in MS Access Database(not with parameters)
我想在 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();
}
此代码有效,但我不想在数据库中看到“0”,我想看到 null(空白)。
当我尝试 Convert.ToDouble(null) 时它抛出一个错误,当我尝试给 DBNull.Value 它抛出一个 "DB missmatch criteria" 错误。
我只需要知道如何使用这段代码将 null 插入数据库。
不想使用 ;
cmd.Parameters.AddWithValue("Scaling", Double.TryParse(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString(), out result) ? (object)result : DBNull.Value);,
因为使用速度太慢,
我的数据库设计就是这样;
我正在等待您的回复,谢谢!
你可以通过改变这一行来像这样使用
+ "'" + 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 ; -- "'
我想在 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();
}
此代码有效,但我不想在数据库中看到“0”,我想看到 null(空白)。
当我尝试 Convert.ToDouble(null) 时它抛出一个错误,当我尝试给 DBNull.Value 它抛出一个 "DB missmatch criteria" 错误。
我只需要知道如何使用这段代码将 null 插入数据库。
不想使用 ;
cmd.Parameters.AddWithValue("Scaling", Double.TryParse(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString(), out result) ? (object)result : DBNull.Value);,
因为使用速度太慢,
我的数据库设计就是这样;
我正在等待您的回复,谢谢!
你可以通过改变这一行来像这样使用
+ "'" + 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 ; -- "'