USING 语句和 TableAdapters 连接

发布于 2024-12-14 18:29:33 字数 1053 浏览 3 评论 0原文

我需要一些帮助来正确实现使用强类型 TableAdapters 的 USING 语句

我有这样的事情:

Using myDT As New mbr_Account.mbr_AccountDataTable
     Using myTA As New mbr_AccountTableAdapters.mbr_AccountTableAdapter
           myTA.Connection.Open()
           myTA.Fill(myDT)
           myTA.Connection.Close()
     End Using

     For Each row In myDT
          'do stuff
     Next
End Using

这将正确处理数据表和表适配器,但不能解决连接对象的问题。

我应该如何处理 Connection 对象?

我可以将连接包装在 Try...Finally 中,如下所示:

Using myDT As New mbr_Account.mbr_AccountDataTable
    Using myTA As New mbr_AccountTableAdapters.mbr_AccountTableAdapter
        Try
            myTA.Connection.Open()
            myTA.Fill(myDT)
        Finally
            If Not IsNothing(myTA.Connection) Then
                myTA.Connection.Close()
                myTA.Connection.Dispose()
            End If
        End Try
    End Using

    For Each row In myDT
        'do stuff
    Next
End Using

问题:如何使用 USING 关键字而不是 Try..Finally 作为连接对象?

I need some help with correctly implementing the USING statement with strongly typed TableAdapters

I have something like this:

Using myDT As New mbr_Account.mbr_AccountDataTable
     Using myTA As New mbr_AccountTableAdapters.mbr_AccountTableAdapter
           myTA.Connection.Open()
           myTA.Fill(myDT)
           myTA.Connection.Close()
     End Using

     For Each row In myDT
          'do stuff
     Next
End Using

This would correctly dispose of the datatable and tableadapter but does not address the issue of the connection object.

How should I dispose the Connection object?

I could wrap the connection in a Try... Finally like this:

Using myDT As New mbr_Account.mbr_AccountDataTable
    Using myTA As New mbr_AccountTableAdapters.mbr_AccountTableAdapter
        Try
            myTA.Connection.Open()
            myTA.Fill(myDT)
        Finally
            If Not IsNothing(myTA.Connection) Then
                myTA.Connection.Close()
                myTA.Connection.Dispose()
            End If
        End Try
    End Using

    For Each row In myDT
        'do stuff
    Next
End Using

Question : How can I use the USING keyword instead of Try.. Finally for the connection object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

独﹏钓一江月 2024-12-21 18:29:50

我刚刚发现 TableAdapters 自动处理连接的打开和关闭,无需手动添加代码。

基本上,它们已经包含 Try ...Finally 块来处理异常期间连接的关闭。

Designer 生成的插入/删除/更新代码如下所示:

global::System.Data.ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
    if (((this.Adapter.InsertCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                != global::System.Data.ConnectionState.Open)) {
        this.Adapter.InsertCommand.Connection.Open();
    }
    try {
        int returnValue = this.Adapter.InsertCommand.ExecuteNonQuery();
        return returnValue;
    }
    finally {
        if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
            this.Adapter.InsertCommand.Connection.Close();
        }
    }

因此无需关闭或处置连接对象。

I have just found out that TableAdapters handle opening and closing of connections automatically and there is no need to manually add the code.

Basically, they already contain Try ... Finally blocks to handle closing of connections during exceptions.

The Designer generated Insert/Delete/Update code looks like this:

global::System.Data.ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
    if (((this.Adapter.InsertCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                != global::System.Data.ConnectionState.Open)) {
        this.Adapter.InsertCommand.Connection.Open();
    }
    try {
        int returnValue = this.Adapter.InsertCommand.ExecuteNonQuery();
        return returnValue;
    }
    finally {
        if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
            this.Adapter.InsertCommand.Connection.Close();
        }
    }

Therefore no need to close or dispose the connection object.

终弃我 2024-12-21 18:29:48

您无法使用 DataReader 的任何原因因此?

Dim sql As String = "SELECT whatever FROM SomeTable"
Using myConnection As New SqlConnection(MyConnectionstring)
    Using myCommand As New SqlCommand(sql, myConnection)
        myConnection.Open()
        Using myReader As SqlDataReader = myCommand.ExecuteReader()
            Dim myTable As New DataTable()
            myTable.Load(myReader)
            myConnection.Close()
            Return myTable
        End Using
    End Using
End Using

连接将被自动关闭并释放。

Any reason you can't use a DataReader thus?

Dim sql As String = "SELECT whatever FROM SomeTable"
Using myConnection As New SqlConnection(MyConnectionstring)
    Using myCommand As New SqlCommand(sql, myConnection)
        myConnection.Open()
        Using myReader As SqlDataReader = myCommand.ExecuteReader()
            Dim myTable As New DataTable()
            myTable.Load(myReader)
            myConnection.Close()
            Return myTable
        End Using
    End Using
End Using

The connection will be closed and disposed of automatically.

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