OracleCommand Update 不提交更改
我正在使用以下代码来更新 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")`
I am using the following code to update a table in oracle. It executes but the update does not commit. If I run the query with Oracle SQL Developer it works fine. What am I missing? SELECT statements work as expected.
`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")`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢大家为我指明了正确的方向。这是最终的工作代码。诀窍是使用 OracleTransaction,下面的代码就像一个魅力。接下来是参数使用。可以在此处找到更多信息
Thank you all for pointing me out in the right direction. This is the final working code. The trick was using the OracleTransaction, the code below works like a charm. Parameter use are next. More information can be found here
我正在从事的项目也使用 OracleCommand。最大的区别是我们所有的数据库调用都转到存储过程而不是直接更改数据库的动态sql。最有可能的是,您需要将更新语句包装在“BEGIN”和“END”块中,并且可能添加一个“COMMIT;”陈述。
不过要小心。如果将回车符和换行符放入 CommandText,则至少有一个版本的 Oracle .NET 库会失败。只需用分号分隔即可。
The project I'm working on uses OracleCommand as well. The big difference is that all of our database calls go to stored procedures rather than dynamic sql that directly changes the database. Most likely, you'll need to wrap the update statement in a "BEGIN" and "END" block, and probably add a "COMMIT;" statement.
Look out though. At least one version of the .NET library for Oracle will fail if you put carriage returns and line feeds into CommandText. Just separate things with the semicolons.