0

これは私が実行しようとしているコードです。エラーなしで実行されますが、データベースが更新されません。

パラメータ化されていない場合は機能しますが、パラメータを追加すると動作し始めます。これが問題のコードです。

Public Sub updateItem()

    Dim sqlConnection1 As New OleDb.OleDbConnection(dbProvider + dbSource)
    Dim cmd As New OleDb.OleDbCommand

    cmd.CommandText = "Update Inventory set PartNumber='@PartNumber', Brand='@Brand', PartDescription='@PartDescription', PartCost=@PartCost, InventoryOnHand=@InventoryOnHand, PartSupplier='@PartSupplier' where PartNumber = '@PartNumMatch' and Brand = '@PartManMatch';"
    cmd.Parameters.AddWithValue("@PartNumber", partNumberText.Text().ToUpper())
    cmd.Parameters.AddWithValue("@Brand", ManufacturerText.Text())
    cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text())
    cmd.Parameters.AddWithValue("@PartCost", Convert.ToDouble(partCostText.Text()))
    cmd.Parameters.AddWithValue("@InventoryOnHand", Convert.ToInt32(quantityText.Text()))
    cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text())
    cmd.Parameters.AddWithValue("@PartNumMatch", partNumberText.Text().ToUpper().Trim())
    cmd.Parameters.AddWithValue("@PartManMatch", ManufacturerText.Text().ToUpper().Trim())


    cmd.CommandType = CommandType.Text
    cmd.Connection = sqlConnection1

    Try

        sqlConnection1.Open()

        cmd.ExecuteNonQuery()

        sqlConnection1.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
        sqlConnection1.Close()
    End Try

    'SQl statement to try to update the selected row's data matched against the database.

    'update listview here.
End Sub

挿入が機能するため、構文が正しいことはほぼ確実です。ここに私の挿入するコードがあります。

Private Sub addItem()
    'SQL statement here to add the item into the database, if successful, move the information entered to listview.

    Dim sqlConnection1 As New OleDb.OleDbConnection(dbProvider + dbSource)
    Dim cmd As New OleDb.OleDbCommand
    'Dim reader As SqlDataReader

    cmd.CommandText = "Insert into Inventory ([PartNumber], [Brand], [PartDescription], [PartCost], [InventoryOnHand], [PartSupplier]) values (@PartNumber, @Brand, @PartDescription, @PartCost, @InventoryOnHand, @PartSupplier);"
    cmd.Parameters.AddWithValue("@PartNumber", partNumberText.Text().ToUpper().Trim())
    cmd.Parameters.AddWithValue("@Brand", ManufacturerText.Text().ToUpper().Trim())
    cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text().Trim())
    cmd.Parameters.AddWithValue("@PartCost", partCostText.Text())
    cmd.Parameters.AddWithValue("@InventoryOnHand", quantityText.Text())
    cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text().Trim())
    cmd.CommandType = CommandType.Text
    cmd.Connection = sqlConnection1
    Dim found As Boolean = False

    Try
        sqlConnection1.Open()

        cmd.ExecuteNonQuery()

        MessageBox.Show(cmd.CommandText)


        sqlConnection1.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
        sqlConnection1.Close()
    End Try

End Sub

where句が正しいことはわかっています。値をハードコーディングし、値をプッシュしてメッセージボックスと比較し、それらをデータベース内の情報と直接比較しました。

あらゆる意見をお寄せいただきありがとうございます。解決できることを願っています。

4

2 に答える 2

0

これを試して、

cmd.CommandText = "Update Inventory set PartNumber=@PartNumber, Brand=@Brand, " + 
                  "PartDescription=@PartDescription, PartCost=@PartCost, " + 
                  "InventoryOnHand=@InventoryOnHand, PartSupplier=@PartSupplier " +  
                  "where PartNumber = @PartNumMatch and Brand = @PartManMatch;"

cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text())
cmd.Parameters.AddWithValue("@PartCost", Convert.ToDouble(partCostText.Text()))
cmd.Parameters.AddWithValue("@InventoryOnHand", Convert.ToInt32(quantityText.Text()))
cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text())
cmd.Parameters.AddWithValue("@PartNumMatch", partNumberText.Text().ToUpper().Trim())
cmd.Parameters.AddWithValue("@PartManMatch", ManufacturerText.Text().ToUpper().Trim())
于 2013-10-11T06:26:13.407 に答える