【发布时间】:2011-03-19 13:44:04
【问题描述】:
我有一个表,我在其中将Datetime 添加到某些列中。我使用存储过程将值插入表中。在存储过程中,我有接受 null 以插入表中的变量。我的问题是当我尝试在我的表列中插入一个空值时,我在列中得到 1900-01-01。我该怎么做而不是在列中只插入 NULL 而不是这个默认值?
这是我的 SP:
CREATE PROCEDURE dbo.Insert
@InserID int,
@InsertDate Datetime = null,
AS
Insert into Tables(InsertID, InsertDate)
Values(@InsertID, @InsertDate)
我这样做是为了分配一个空值:
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;
}
添加到我的表列的值不是 NULL,它是 1900-01-01。
我该怎么办?
【问题讨论】:
-
列
InsertDate是否在表定义中指定了默认值? -
@Brook:这将是一个很好的答案!
-
InsertDate 既可以是 null 也可以是有效日期。
标签: c# sql sql-server