ODP.NET 代码示例批评或最佳实践

发布于 2024-07-30 11:49:36 字数 1359 浏览 1 评论 0原文

我目前在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-08-06 11:49:36

有几件事

  • 我肯定会将命令创建代码移出到它自己的方法中。 无论如何,您可能会重复使用它,因此请避免复制和复制它。 粘贴。
  • 您不需要在命令周围使用 using ,因为无论如何您都在整个连接上使用它。 (不过这样做没有坏处)
  • 一次查询事务? 那有什么用呢?
  • 您打开连接两次。

这是我正在使用的一些代码。

 Public Function QueryDataTable(ByVal storedProcedureName As String, ByVal ParamArray parameters As IDbDataParameter()) As DataTable
        Try
            Dim command As SqlCommand = CreateCommand(connection, storedProcedureName, parameters)
            Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(command)
            Dim dtResult As New DataTable()

        If transaction Is Nothing Then connection.Open()
        adapter.Fill(dtResult)
        Return dtResult
    Catch ex As Exception
        Log.Append("SQL", "QueryDataTable", ex)
        Throw
    Finally
        If transaction Is Nothing Then connection.Close()
    End Try
End Function

上面代码中的主要内容

  • 我已经将命令的创建分解为 CreateCommand 方法。
  • 当我在实例级别实现事务时,连接对象也必须在实例级别,因此我不能使用 using,而是必须使用 try-finally 以确保连接关闭(当没有事务时)在跑)。
  • 对于事务,我实现 IDisposable 并在整个事务期间保持打开的连接。 手动实现事务的替代方法是使用 System.Transactions 命名空间,具体取决于您的目标数据库、它所具有的支持以及您需要的事务控制级别。

无论如何,以上内容使得无论有交易还是没有交易都可以轻松工作。 上面的简单交易看起来像这样:

Using sql = new MySqlWrapper(transactionLevel)
   dim dt as DataTable = sql.QueryDataTable(a,b)
   if EverythingOK(dt) then sql.CommitTransaction()
End Using

当不需要交易时,我可以简单地做

dim dt as DataTable = new MySqlWrapper().sql.QueryDataTable(a,b)

A few things

  • I would definitly move out the command creation code to its own method. You are probably reusing it anyway, so avoid the copy & pasting.
  • You shouldn't need to use using around the command as you are using it on the whole connection anyway. (no harm in doing so though)
  • A one query transaction? What is the use in that?
  • You open the connection twice.

Here is some code I am using.

 Public Function QueryDataTable(ByVal storedProcedureName As String, ByVal ParamArray parameters As IDbDataParameter()) As DataTable
        Try
            Dim command As SqlCommand = CreateCommand(connection, storedProcedureName, parameters)
            Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(command)
            Dim dtResult As New DataTable()

        If transaction Is Nothing Then connection.Open()
        adapter.Fill(dtResult)
        Return dtResult
    Catch ex As Exception
        Log.Append("SQL", "QueryDataTable", ex)
        Throw
    Finally
        If transaction Is Nothing Then connection.Close()
    End Try
End Function

The main things in the above code

  • I have broken out the creation of the command to a CreateCommand method.
  • As I am implementing transactions on the instance level, the connection object also has to be on an instance level, so I can't use using but instead have to go with to try-finally to ensure that the connection gets closed (when no transaction is running).
  • For transactions I implement IDisposable and maintain an open connection during the whole transaction. An alternative to manually implementing transactions would be to make use of the System.Transactions namespace, depending on which database you are targetting, the support it has, and the level of transaction control you need.

Anyway, the above makes it easy to work both with and without transactions. A simple transaction from above would look something like this:

Using sql = new MySqlWrapper(transactionLevel)
   dim dt as DataTable = sql.QueryDataTable(a,b)
   if EverythingOK(dt) then sql.CommitTransaction()
End Using

And when no transactions are needed, I can simply do

dim dt as DataTable = new MySqlWrapper().sql.QueryDataTable(a,b)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文