OracleCommand Update不提交更改

时间:2010-10-11 16:03:54

标签: c# oraclecommand

我使用以下代码更新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")`

2 个答案:

答案 0 :(得分:2)

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

            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

答案 1 :(得分:0)

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

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