Sql 允许在数据库中保存空值

Sql 允许在数据库中保存空值,sql,vb.net,visual-studio,visual-studio-2003,Sql,Vb.net,Visual Studio,Visual Studio 2003,Im使用Visual Studio.NET 2003当所有textbox、combobox字段都填充数据时,“添加”按钮工作正常,但在测试时,如果不使用数据填充字段,则返回空值,并返回错误:“字符串未被识别为有效的日期时间” 我有一个名为txtPurchasedDate的文本框 我的存储过程 CREATE PROCEDURE AddOfficeEquipmentProfile ( @OE_ID varchar(11) = NULL, @OE_Cat

Im使用Visual Studio.NET 2003当所有textbox、combobox字段都填充数据时,“添加”按钮工作正常,但在测试时,如果不使用数据填充字段,则返回空值,并返回错误:“字符串未被识别为有效的日期时间”

我有一个名为txtPurchasedDate的文本框

我的存储过程

 CREATE PROCEDURE AddOfficeEquipmentProfile
    (
    @OE_ID      varchar(11)     =   NULL,
    @OE_Category        char(3)         =   NULL,
    @OE_SubCategory char(3)         =   NULL,
    @OE_Name        varchar(35)     =   NULL,
    @OE_User        varchar(35)     =   NULL,
    @OE_Brand       varchar(15)     =   NULL,
    @OE_Model       varchar(35)     =   NULL,
    @OE_Specs       varchar(1000)       =   NULL,
    @OE_SerialNo        varchar(35)     =   NULL,
    @OE_PropertyNo  varchar(35)     =   NULL,
    @OE_MacAddress  varchar(100)        =   NULL,   
    @OE_Static_IP       varchar(15)     =   NULL,
    @OE_Vendor      varchar(35)     =   NULL,
    @OE_PurchaseDate    smalldatetime       =   NULL,
    @OE_WarrantyInclusiveYear   int     =   NULL,
    @OE_WarrantyStatus  char(2)         =   NULL,
    @OE_Status      varchar(15)     =   NULL,
    @OE_Dept_Code   char(3)         =   NULL,
    @OE_Location_Code   char(8)         =   NULL,
    @OE_Remarks     varchar(1000)       =   NULL
    )
    AS

    INSERT INTO tblOfficeEquipmentProfile (OE_ID, OE_Category, OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo,
    OE_PropertyNo, OE_MacAddress, OE_Static_IP, OE_Vendor, OE_PurchaseDate, OE_WarrantyInclusiveYear, OE_WarrantyStatus, OE_Status, OE_Dept_Code,
    OE_Location_Code, OE_Remarks ) 
    VALUES (@OE_ID, @OE_Category, @OE_SubCategory, @OE_Name, @OE_User, @OE_Brand, @OE_Model, 
    @OE_Specs, @OE_SerialNo, @OE_PropertyNo, @OE_MacAddress, @OE_Static_IP, @OE_Vendor, @OE_PurchaseDate, @OE_WarrantyInclusiveYear, @OE_WarrantyStatus,
    @OE_Status, @OE_Dept_Code, @OE_Location_Code, @OE_Remarks)

    IF @@ERROR<>0
        BEGIN
            ROLLBACK TRANSACTION
            RETURN 0
        END 
    ELSE
        BEGIN
            COMMIT TRANSACTION
            RETURN 1
        END
    GO

如果要存储空值,则必须在vb代码中使用条件逻辑,不将这些参数发送到存储过程,或者发送dbnull值。甚至您的char字段也存储空字符串,这可能不是您的意图。

使用

If (Not String.IsNullOrEmpty(txtPurchaseDate.Text)) Then
    cmd.Parameters("@OE_PurchaseDate").Value = txtPurchaseDate.Text
End If

为什么不为PurchaseDate使用DateTimePicker,而不是txtPurchaseDate中的文本框。文本您可以将控件命名为dtpPurchaseDate,这样您就有了:

cmd.Parameters("@OE_PurchaseDate").Value = dtpPurchaseDate.Value
日期时间选择器的默认值(或最小值)为1/1/1980,因此可以将dtpPurchaseDate的值初始化为1/1/1980,如下所示:

dtpPurchaseDate = System.DateTime.Parse("1/1/1980")
顺便说一句,当它存储在数据库中时,它的值为1/1/1980,而不是NULL

如果您想使用文本框,则可以检查是否为空,然后指定一个最小日期值,该值实际上为1/1/0001,如下所示:

If (If (String.IsNullOrEmpty(txtPurchaseDate.Text)) Then
   cmd.Parameters("@OE_PurchaseDate").Value = DBNull.Value  
Else
   Dim tempPurchaseDate as DateTime

   System.DateTime.TryParse(txtPurchaseDate.Text, tempPurchaseDate)
   cmd.Parameters("@OE_PurchaseDate").Value = tempPurchaseDate         
End If
但它在数据库中的存储值为1/1/0001

您可以在文本框中将其显示为空,如: 假设您有一个名为dr的数据行

If (dr["OE_PurchaseDate"] = System.DateTime.Parse("1/1/0001")) Then
   txtPurchaseDate.Text = ""
Else
   txtPurchaseDate.Text = dr["OE_PurchaseDate"]
End If

您要传递的PurchaseDate是什么格式?如果要传递空日期,则需要确保数据库设置为允许空值。每个列都可以设置为允许空值。@Jason是的,先生它允许空值我检查了我的数据库我该怎么办then@Jason,他正确地做了那部分。他将PurchaseDate参数作为smalldatetime传递,使格式变得无关。日期和时间的唯一时间格式是显示日期和时间的时间。您计划问多少次相同(或类似的问题)?、,这一个…?语法和逻辑如何,先生?@JohnFx如果我还没有想法,已经尝试过dbNULL,我就不会费心问这个问题了。我先尝试不同的事情,然后在发布之前搜索谷歌。他已经告诉你答案,现在你要他把答案打印出来。也许我不是像你一样的天才,先生@JohnFx,你可以理解每一件小事,因为你不努力,而不是缺乏理解。答案为空(txtPurchaseDate.Text)检查可能不是绝对必要的-我认为txtPurchaseDate.Text将是空字符串“”,如果没有设置。您可以通过在运行时检查值来检查这一点。虽然它可能比不包含check更安全。IsNull未声明为sir抱歉,我给出的代码是VBA。对于VB,您可以使用String.IsNullOrEmpty(txtPurchaseDate.Text)作为检查。String.IsNullOrEmpty在1.1中不可用-根据问题的标记,OP使用VS 2003。然后在使用日期时间选择器时使用txtPurchaseDate.Text“”,如果我不更改任何内容,默认日期会是什么?
If (dr["OE_PurchaseDate"] = System.DateTime.Parse("1/1/0001")) Then
   txtPurchaseDate.Text = ""
Else
   txtPurchaseDate.Text = dr["OE_PurchaseDate"]
End If