1

我正在使用以下代码更新 oracle 中的表。它执行但更新不提交。如果我使用 Oracle SQL Developer 运行查询,它工作正常。我错过了什么?SELECT 语句按预期工作。

`Dim BillOfLadingNumber As String = txtBillOfLadingNumber.Text.Trim

    Dim TrailerNumber As String = txtTrailerNumber.Text.Trim
    Dim CarrierCode As String = txtCarrierCode.Text.Trim
   Dim TransportationMethod As String = txtTransportationMethod.Text.Trim 
   Dim OracleCommand As New OracleCommand()
    With OracleCommand
        .Connection = OracleConnection
        .CommandType = CommandType.Text
        .CommandText = "UPDATE XXF_ASN_HEADERS SET BILL_OF_LADING_NUMBER ='" + BillOfLadingNumber + "',TRAILER_NUMBER ='" + TrailerNumber + "',CARRIER_CODE ='" + CarrierCode + "',TRANSPORTATION_METHOD ='" + TransportationMethod + "' WHERE HEADERID ='" + Request.QueryString("HeaderId") + "'"
    End With

    OracleConnection.Open()
    Dim result As Integer = OracleCommand.ExecuteNonQuery()
    OracleConnection.Close()

    If result = 1 Then Response.Redirect("default.aspx")`
4

2 回答 2

2

谢谢大家指出我正确的方向。这是最终的工作代码。诀窍是使用 OracleTransaction,下面的代码就像一个魅力。接下来是参数使用。更多信息可以在这里找到

            Using dbConnection As New OracleConnection(OracleConnectionString)

                'Open the connection
                dbConnection.Open()

                Dim dbCommand As OracleCommand = dbConnection.CreateCommand()
                Dim dbTransaction As OracleTransaction

                'Start a local transaction 
                dbTransaction = dbConnection.BeginTransaction(IsolationLevel.ReadCommitted)
                'Assign transaction object for a pending local transaction
                dbCommand.Transaction = dbTransaction

                Try
                    dbCommand.CommandType = CommandType.Text
                    dbCommand.CommandText = "UPDATE XXF_ASN_HEADERS SET BILL_OF_LADING_NUMBER ='" + BillOfLadingNumber + "', TRAILER_NUMBER ='" + TrailerNumber + "', CARRIER_CODE ='" + CarrierCode + "', TRANSPORTATION_METHOD ='" + TransportationMethod + "' WHERE HEADERID ='" + Request.QueryString("HeaderId") + "'"
                    dbCommand.ExecuteScalar()
                    dbTransaction.Commit()
                    Response.Redirect("default.aspx")
                Catch ex As OracleException
                    'Rollback the transaction
                    dbTransaction.Rollback()
                    'display error details
                    lblUpdateQuery.Text = dbCommand.CommandText
                    lblDebug.Text = ex.Message.ToString
                End Try

            End Using
于 2010-10-12T03:13:58.907 回答
0

我正在处理的项目也使用 OracleCommand。最大的不同是我们所有的数据库调用都转到存储过程,而不是直接更改数据库的动态 sql。最有可能的是,您需要将更新语句包装在“BEGIN”和“END”块中,并可能添加“COMMIT;” 陈述。

不过请注意。如果您将回车和换行符放入 CommandText,则至少有一个版本的 Oracle .NET 库将失败。只需用分号分隔事物。

于 2010-10-11T16:09:58.873 回答