【问题标题】:Allow Null values to be saved in database允许将 Null 值保存在数据库中
【发布时间】:2013-04-26 01:51:44
【问题描述】:

我使用 Visual Studio .NET 2003,当所有文本框、组合框字段都填充有数据时,添加按钮可以正常工作,但是在测试不填充字段时将其保留为 NULL,它返回错误提示“字符串未被识别为有效日期时间”

我有一个名为 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.net 添加按钮代码

 Dim cmd As SqlCommand = sqlconn.CreateCommand
        sqlconn.Open()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "AddOfficeEquipmentProfile"

    cmd.Parameters.Add("@OE_ID", SqlDbType.VarChar, 11, "oeq-su-999")
    cmd.Parameters.Add("@OE_Category", SqlDbType.Char, 3, "COM")
    cmd.Parameters.Add("@OE_SubCategory", SqlDbType.Char, 3, "SU")
    cmd.Parameters.Add("@OE_Name", SqlDbType.VarChar, 35, "adminpmis01")
    cmd.Parameters.Add("@OE_User", SqlDbType.VarChar, 35, "Ivan")
    cmd.Parameters.Add("@OE_Brand", SqlDbType.VarChar, 15, "DELL")
    cmd.Parameters.Add("@OE_Model", SqlDbType.VarChar, 35, "optiplex")
    cmd.Parameters.Add("@OE_Specs", SqlDbType.VarChar, 1000, "dualcore")
    cmd.Parameters.Add("@OE_SerialNo", SqlDbType.VarChar, 35, "sgh5960")
    cmd.Parameters.Add("@OE_PropertyNo", SqlDbType.VarChar, 35, "j7h7h6g6f2")
    cmd.Parameters.Add("@OE_MacAddress", SqlDbType.VarChar, 100, "j7h7:h6g6f2")
    cmd.Parameters.Add("@OE_Static_IP", SqlDbType.VarChar, 15, "192.168.1.5")
    cmd.Parameters.Add("@OE_Vendor", SqlDbType.VarChar, 35, "ADWAYS")

    cmd.Parameters.Add("@OE_PurchaseDate", SqlDbType.SmallDateTime)
    cmd.Parameters.Add("@OE_WarrantyInclusiveYear", SqlDbType.Int)
    cmd.Parameters.Add("@OE_WarrantyStatus", SqlDbType.Char, 2, "IN")
    cmd.Parameters.Add("@OE_Status", SqlDbType.VarChar, 15, "Good")
    cmd.Parameters.Add("@OE_Dept_Code", SqlDbType.Char, 3, "ADM")
    cmd.Parameters.Add("@OE_Location_Code", SqlDbType.Char, 8, "ADM_OFC")
    cmd.Parameters.Add("@OE_Remarks", SqlDbType.VarChar, 1000, "ACTIVE")
    cmd.Parameters("@OE_ID").Value = txtOEID.Text
    cmd.Parameters("@OE_Category").Value = cmbCategory.Text
    cmd.Parameters("@OE_SubCategory").Value = cmbSubCategory.Text
    cmd.Parameters("@OE_Name").Value = txtName.Text
    cmd.Parameters("@OE_User").Value = txtUser.Text
    cmd.Parameters("@OE_Brand").Value = cmbBrand.Text
    cmd.Parameters("@OE_Model").Value = cmbModel.Text
    cmd.Parameters("@OE_Specs").Value = txtSpecs.Text
    cmd.Parameters("@OE_SerialNo").Value = txtSerialNo.Text
    cmd.Parameters("@OE_PropertyNo").Value = txtPropertyNo.Text
    cmd.Parameters("@OE_MacAddress").Value = txtMacAddress.Text
    cmd.Parameters("@OE_Static_IP").Value = txtStaticIp.Text
    cmd.Parameters("@OE_Vendor").Value = txtVendor.Text
    cmd.Parameters("@OE_PurchaseDate").Value = txtPurchaseDate.Text
    cmd.Parameters("@OE_WarrantyInclusiveYear").Value = txtWarrantyInclusiveYear.Text
    cmd.Parameters("@OE_WarrantyStatus").Value = txtWarrantyStatus.Text
    cmd.Parameters("@OE_Status").Value = txtStatus.Text
    cmd.Parameters("@OE_Dept_Code").Value = cmbDeptCode.Text
    cmd.Parameters("@OE_Location_Code").Value = cmbLocationCode.Text
    cmd.Parameters("@OE_Remarks").Value = txtRemarks.Text
    cmd.ExecuteNonQuery()
    MsgBox("Successfully Added Equipment Profile")
    sqlconn.Close()

【问题讨论】:

  • 您传入的 PurchaseDate 格式是什么?
  • 如果你传入一个空日期,你需要确保你的数据库设置为允许空值。每列都可以设置为允许空值。
  • @Jason 是的,先生,它允许空值我检查了我的数据库,然后我该怎么办
  • @Jason,他做得对。他将自己的 PurchaseDate 参数作为 smalldatetime 传递,从而使格式无关紧要。日期和时间唯一重要的时间格式是显示它们的时间。
  • 您打算问多少次相同(或类似的问题)? stackoverflow.com/q/16232173/745969,stackoverflow.com/q/16291644/745969,stackoverflow.com/q/16293543/745969,这个……?

标签: sql vb.net visual-studio visual-studio-2003


【解决方案1】:

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

【讨论】:

  • 先生的语法和逻辑如何?
  • @JohnFx 如果我没有用尽想法,我不会费心问问题,已经尝试过 dbNULL。我先尝试不同的东西,然后在发布之前研究谷歌
  • 他已经告诉你答案了,现在你让他打出来。
  • 好吧,也许我不像@JohnFx先生那样天才,你可以理解每一件小事
  • 因缺乏努力而不是缺乏理解而呼吁您。
【解决方案2】:

使用

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

【讨论】:

  • IsNull(txtPurchaseDate.Text) 检查可能不是绝对必要的 - 我认为 txtPurchaseDate.Text 将是空字符串,如果未设置,则为“”。您可以通过在运行时检查该值来检查这一点。虽然它可能比不包括检查更安全。
  • IsNull 没有被声明为先生
  • 对不起,我给的代码是VBA。对于 VB,您可以使用 String.IsNullOrEmpty(txtPurchaseDate.Text) 作为检查。
  • String.IsNullOrEmpty 在 1.1 中不可用 - 根据问题的标签,OP 正在使用 VS 2003。
  • 然后使用txtPurchaseDate.Text ""
【解决方案3】:

为什么不使用 DateTimePicker 作为您的 PurchaseDate 而不是 txtPurchaseDate.Text 中的文本框,您可以将控件命名为 dtpPurchaseDate,然后您就有了:

cmd.Parameters("@OE_PurchaseDate").Value = dtpPurchaseDate.Value

DateTime Picker 的默认值(或最小值)是 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

您可以在 TextBox 中将其显示为空,例如: 假设您有一个名为 dr 的 DataRow

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

【讨论】:

  • 在使用日期时间选择器时,如果我不管它什么都不改变默认日期会怎样?
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 2020-12-26
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
相关资源
最近更新 更多