从数据库中检索条目,其中时间值为空

时间:2014-05-07 20:30:52

标签: c# database

我正在尝试更新数据库中的一行,其中某个日期/时间值为空。

我下面的代码给出了错误: - 参数?_2没有默认值。

有谁知道如何使用null / empty值?

干杯

OleDbCommand com = new OleDbCommand("SELECT count(*) from DI WHERE [Task] = ? AND [Time_Off] = ?", Program.DB_CONNECTION);
com.Parameters.Add(new OleDbParameter("", "DI"));
com.Parameters.Add(new OleDbParameter("", null));
int count = (int)com.ExecuteScalar();

if (count > 0)
{
    OleDbCommand com1 = new OleDbCommand("UPDATE DI SET [Time_OFF] = ? WHERE [Task] = ? AND [Time_Off] = ?", Program.DB_CONNECTION);
    com1.Parameters.Add(new OleDbParameter("", DateTime.Now.TimeOfDay));
    com1.Parameters.Add(new OleDbParameter("", "DI"));
    com1.Parameters.Add(new OleDbParameter("", null));
    com1.ExecuteNonQuery();
}
else
{
    DIPerson = DIlist[DIcomboBox.SelectedIndex];

    OleDbCommand com2 = new OleDbCommand("INSERT INTO DI ([Person], [Task], [Time_On]) VALUES (?, ?, ?)", Program.DB_CONNECTION);
    com2.Parameters.Add(new OleDbParameter("", DIPerson.ID));
    com2.Parameters.Add(new OleDbParameter("", "DI"));
    com2.Parameters.Add(new OleDbParameter("", DateTime.Now.TimeOfDay));

    com2.ExecuteNonQuery();
}

1 个答案:

答案 0 :(得分:0)

OleDbCommand com = new OleDbCommand("SELECT count(*) from DI WHERE [Task] = ? AND [Time_Off] is null", Program.DB_CONNECTION);
com.Parameters.Add(new OleDbParameter("", "DI"));
int count = (int)com.ExecuteScalar();

if (count > 0)
{
    OleDbCommand com1 = new OleDbCommand("UPDATE DI SET [Time_OFF] = ? WHERE [Task] = ? AND [Time_Off] is null", Program.DB_CONNECTION);
    com1.Parameters.Add(new OleDbParameter("", DateTime.Now.TimeOfDay));
    com1.Parameters.Add(new OleDbParameter("", "DI"));
    com1.ExecuteNonQuery();
}
else
{
    DIPerson = DIlist[DIcomboBox.SelectedIndex];

    OleDbCommand com2 = new OleDbCommand("INSERT INTO DI ([Person], [Task], [Time_On]) VALUES     (?, ?, ?)", Program.DB_CONNECTION);
    com2.Parameters.Add(new OleDbParameter("", DIPerson.ID));
    com2.Parameters.Add(new OleDbParameter("", "DI"));
    com2.Parameters.Add(new OleDbParameter("", DateTime.Now.TimeOfDay));

    com2.ExecuteNonQuery();
}