更新时允许空值,使用SQL Server存储过程将记录插入数据库

时间:2013-04-26 08:42:50

标签: sql sql-server vb.net parameters

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

    'declare the variables
    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()

当没有空值时,我的插入和更新存储过程正在工作,但是当我需要使某些列真的为空值时呢?

我在vb.net 2003中使用SQL Server存储过程

有关代码示例的任何建议

2 个答案:

答案 0 :(得分:3)

您可以使用DBNull.Value作为存储过程的参数。确保您的表允许字段为空值。

例如,如果您希望OE_Brandnull,则可以执行以下操作:

cmd.Parameters("@OE_Brand").Value = DBNull.Value

答案 1 :(得分:3)

将其设置为Nothing将取消参数。请改用DBNull.Value

cmd.Parameters("@OE_Remarks").Value = DBNull.Value