如何在 SQL Server 中向 datetime 类型的列插入 null 值

13

我有一张表格,其中有几个日期时间类型的列。我使用存储过程向表中插入值。在存储过程中,我有变量可接受null值以插入到表中。我的问题是当我尝试将null值插入到表格的日期时间列中时,它会报错。 我的代码如下。

    System.Data.SqlTypes.SqlDateTime getDate;
//set DateTime null
getDate = SqlDateTime.Null;

if (InsertDate.Text == "")
{
cmd1.Parameters["@InsertDate"].Value = getDate;
}
else
{
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}

3
抛出了哪个错误?该列是否允许空值? - Linky
11个回答

17
cmd1.Parameters["@InsertDate"].Value = getDate ?? DBNull.Value;

关键在于DBNull.Value。我想你有一个可空的DateTime变量,即DateTime? someVariable。您可以测试其是否为null,如果是,则使用DBNull.Value(如上所示)。


9
if (//some condition)   
{
   cmd.Parameters.AddWithValue("@dateofBirth", txtdob.text);
}
else 
{ 
   cmd.Parameters.AddWithValue("@dateofBirth", DBNull.Value); 
}

应该补充说明 //some condition 是指 txtdob.Text,或者在 OP 的情况下是 InsertDate.Text 是否为空... 在这个例子中,if (!String.IsNullOrEmpty(InsertDate.Text)) 就符合要求。 - vapcguy

5

如果要将 null 值传递给数据库,您可以使用 DBNull.Value。请尝试这样做。

 if (string.IsNullOrWhiteSpace(InsertDate.Text))
  {
   cmd1.Parameters["@InsertDate"].Value =DBNull.Value;
  }
 else
  {
   cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
  }

4

尝试使用这个

System.Data.SqlTypes.SqlDateTime.Null

4
请将以下内容翻译为中文:请详细说明代码是如何解决这个问题的。 - Tom Aranda

3
首先,要能够将null插入数据库列中,该列必须接受null。在设计器中打开表格,并查看相关列是否可为空。如果不行并且您尝试插入null值,则会出现以下错误:

无法将值NULL插入到列“InsertDate”中,表“TableName”;该列不允许null值。

既然您正在使用存储过程插入数据,我建议您在过程级别上为参数使用默认值,而不是加重应用程序代码的负担。然后,您只需在应用程序端不设置参数,它就可以正常工作:
 create procedure InsertIntoTable
   -- ....other parameters here...
   @InsertDate datetime = null -- this means that if the value is not supplied, 
                               -- null is used by default
 as begin
   insert into TableName (InsertDate) values (@InsertDate)
 end

在C#端,只需要执行以下操作:
if (!string.IsNullOrWhitespace(InsertDate.Text)) {
  // check to see if the entered text is actually a date
  DateTime insertDate = ... some parsing/verification code...;
  cmd1.Parameters["@InsertDate"].Value = insertDate;
}

请注意,此处没有else分支,因此如果InsertDate为空,则不会发送该参数。

2
尝试像这样使用Add而不是AddWithValue,例如:
DB.Parameters.Add("@date", SqlDbType.DateTime).Value = DBNull.Value;

1

您只需直接将null作为参数值提供:

if (String.IsNullOrWhitespace(InsertDate.Text)) {
    cmd1.Parameters["@InsertDate"].Value = null;
} else {
    cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}

还切换使用了 String.IsNullOrWhitespace() 来检查空字符串。


2
直接使用null可能会导致问题,因为参数可能没有被发送(取决于语句和/或命令类型)...DbNull.Value几乎总是有效的。 - Linky
@Linky 啊。我个人从来没有遇到过这个问题,但知道并记在心中还是很有意思的。 - Lloyd
1
当使用commandType == StoredProcedure的命令并且commandText只是存储过程名称时,参数将被添加到执行的文本中(例如使用Sql Server分析器进行检查)。如果您添加一个值为null的参数,则该参数将不再是执行的SQL的一部分,如果您的存储过程需要此参数,则执行将失败。SWeko的答案也提到了这一点。 如果您正在构建像“Insert into X(col)values(@col)”这样的语句,则据我所知并没有区别。 - Linky

0
在存储过程中做类似于这样的事情:
insert into table(date)
values(case when @InsertDate ='' then 
               null 
            else 
               @insertDate 
       end)

0

尝试使用:

if (string.IsNullOrWhiteSpace(InsertDate.Text))
{
    cmd1.Parameters["@InsertDate"].Value = getDate;
}
else
{
    cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}

这基本上就是他使用的内容,也是导致错误的原因。问题在于它正在发送 SqlDateTime.Null,无论是他还是你,而实际上应该发送 DBNull.Value - vapcguy

0

如果DBNull.Value无法使用,请尝试使用DateTime? null的逻辑。

确保您的数据库字段允许为空。

dtexpiry!= null? dtexpiry:(DateTime?) null


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接