ODP.NET 代码示例批评或最佳实践
我目前在 Vb.Net 中有一个数据访问层。 我对执行查询(作为数据集)和执行非查询函数的实现不太满意。 有人有我可以看到的代码吗? 我的代码看起来不干净。 对此的任何想法或批评也将不胜感激。
Using odpConn As OracleConnection = New OracleConnection(_myConnString)
odpConn.Open()
If _beginTransaction Then
txn = odpConn.BeginTransaction(IsolationLevel.Serializable)
End If
Try
Using odpCmd As OracleCommand = odpConn.CreateCommand()
odpCmd.CommandType = CommandType.Text
odpCmd.CommandText = sSql
For i = 0 To parameters.Parameters.Count - 1
Dim prm As New OracleParameter
prm = DirectCast(parameters.Parameters(i), ICloneable).Clone
odpCmd.Parameters.Add(prm)
Next
If (odpConn.State = ConnectionState.Closed) Then
odpConn.Open()
End If
iToReturn = odpCmd.ExecuteNonQuery()
If _beginTransaction Then
txn.Commit()
End If
End Using
Catch
txn.Rollback()
End Try
End Using
I currently have a DataAccess Layer in Vb.Net. I am not too happy with my implementation of both my ExecuteQuery (as DataSet) and ExecuteNonQuery functions. Does anyone have any code that I could see? My code just doesn't look clean. Any thoughts or critiques on it would be appreciated also.
Using odpConn As OracleConnection = New OracleConnection(_myConnString)
odpConn.Open()
If _beginTransaction Then
txn = odpConn.BeginTransaction(IsolationLevel.Serializable)
End If
Try
Using odpCmd As OracleCommand = odpConn.CreateCommand()
odpCmd.CommandType = CommandType.Text
odpCmd.CommandText = sSql
For i = 0 To parameters.Parameters.Count - 1
Dim prm As New OracleParameter
prm = DirectCast(parameters.Parameters(i), ICloneable).Clone
odpCmd.Parameters.Add(prm)
Next
If (odpConn.State = ConnectionState.Closed) Then
odpConn.Open()
End If
iToReturn = odpCmd.ExecuteNonQuery()
If _beginTransaction Then
txn.Commit()
End If
End Using
Catch
txn.Rollback()
End Try
End Using
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事
这是我正在使用的一些代码。
上面代码中的主要内容
无论如何,以上内容使得无论有交易还是没有交易都可以轻松工作。 上面的简单交易看起来像这样:
当不需要交易时,我可以简单地做
A few things
Here is some code I am using.
The main things in the above code
Anyway, the above makes it easy to work both with and without transactions. A simple transaction from above would look something like this:
And when no transactions are needed, I can simply do