【发布时间】: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