1

このストアド プロシージャを vb.net で実行し、購入テキスト ボックスを空白のままにして、更新時に null にする場合。しかし、String が有効な DateTime として認識されなかったというエラーが返されます。コードを機能させるための提案は大歓迎です。

  CREATE PROCEDURE UpdateEquipmentProfile

    (
    @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

    IF EXISTS(SELECT * FROM tblOfficeEquipmentProfile WHERE OE_ID=@OE_ID)

    UPDATE tblOfficeEquipmentProfile    SET
    OE_Category         =@OE_Category,
    OE_SubCategory      =@OE_SubCategory,
    OE_Name         =@OE_Name,
    OE_User         =@OE_User,
    OE_Brand            =@OE_Brand,
    OE_Model            =@OE_Model,
    OE_Specs            =@OE_Specs,
    OE_SerialNo         =@OE_SerialNo,
    OE_PropertyNo       =@OE_PropertyNo,
    OE_MacAddress       =@OE_MacAddress,
    OE_Static_IP        =@OE_Static_IP,
    OE_Vendor           =@OE_Vendor,
    OE_PurchaseDate     =@OE_PurchaseDate,
    OE_WarrantyInclusiveYear    =@OE_WarrantyInclusiveYear,
    OE_WarrantyStatus       =@OE_WarrantyStatus,
    OE_Status           =@OE_Status,
    OE_Dept_Code        =@OE_Dept_Code,
    OE_Location_Code        =@OE_Location_Code,
    OE_Remarks          =@OE_Remarks

    WHERE OE_ID=@OE_ID
    GO

VB.NET コード

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

    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 Updated Equipment Profile")
    sqlconn.Close()
4

2 に答える 2

1

パラメータのデフォルト値は NULL であるため、次のようになります。

 @OE_PurchaseDate        smalldatetime   =   NULL,

渡す値がない場合、強制的に値を渡す必要はありません

例えば:

if txtPurchaseDate.Text <> "" then
    cmd.Parameters("@OE_PurchaseDate").Value = txtPurchaseDate.Text
于 2013-04-30T06:53:05.583 に答える
0
Dim d as DateTime
DateTime.TryParse(txtPurchaseDate.Text, d)

d次に、ストアド プロシージャ呼び出しに渡します。

cmd.Parameters("@OE_PurchaseDate").Value = d
于 2013-04-30T06:35:50.180 に答える