Asp.Net 中的 ODBC 连接(连接和处置)

发布于 2024-12-06 20:06:19 字数 1197 浏览 0 评论 0原文

我在页面启动并运行时打开和关闭 ODBC 连接。我应该何时何地放置 Connection.Dispose 方法?我尝试了 Page_Dispose,但它从未出现过。

Public Class _WepPage1
Inherits System.Web.UI.Page

Dim MyConnection As New Odbc.OdbcConnection

Private Sub Page_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
    MyConnection.Dispose()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'String comes from Web.Config
    MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
    If Not IsPostBack Then
        Call LoadSomeData()
    Else
        'Do some more stuff            
    End If
End Sub

Private Sub LoadSomeData()
    If MyConnection.State = ConnectionState.Closed Then MyConnection.Open()
    Dim MyCommand As New Odbc.OdbcCommand("select * from tablename", MyConnection)
    Try
        Dim dataR As Odbc.OdbcDataReader = MyCommand.ExecuteReader
        While dataR.Read
            DropDownList1.Items.Add(dataR("column1"))
        End While
    Catch ex As Exception
    End Try
    MyCommand.Dispose()
    If MyConnection.State = ConnectionState.Open Then MyConnection.Close()
End Sub

I Open and Close ODBC connection while the page is up and running. When and where should i put the Connection.Dispose method? I tried on the Page_Disposed but it never makes it there.

Public Class _WepPage1
Inherits System.Web.UI.Page

Dim MyConnection As New Odbc.OdbcConnection

Private Sub Page_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
    MyConnection.Dispose()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'String comes from Web.Config
    MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
    If Not IsPostBack Then
        Call LoadSomeData()
    Else
        'Do some more stuff            
    End If
End Sub

Private Sub LoadSomeData()
    If MyConnection.State = ConnectionState.Closed Then MyConnection.Open()
    Dim MyCommand As New Odbc.OdbcCommand("select * from tablename", MyConnection)
    Try
        Dim dataR As Odbc.OdbcDataReader = MyCommand.ExecuteReader
        While dataR.Read
            DropDownList1.Items.Add(dataR("column1"))
        End While
    Catch ex As Exception
    End Try
    MyCommand.Dispose()
    If MyConnection.State = ConnectionState.Open Then MyConnection.Close()
End Sub

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

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

发布评论

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

评论(1

猥琐帝 2024-12-13 20:06:19

我不会使用 Page_Load 来启动连接。在 .NET 中,当您使用 Dispose() 时,“真正的”连接对象会返回到池中,因此它可以用于其他工作。即使您多次打开、关闭和处置,最大开销也只有几微秒,因此尽早打开连接然后在整个页面持续时间内保持打开状态并不能真正帮助您。

此外,在 Page_Load 中打开连接意味着您已将数据访问与 UI 紧密耦合,从而需要进行大量重新架构,而不仅仅是重新安排以将工作划分为适当的层。

这意味着应该以一种方法创建、使用和处置连接(在您的示例中)。这意味着它在逻辑上是分离的,并且很容易分成不同的类和/或程序集(如果应用程序增长,您需要完整的 DAL)。

我至少会将所有房地产移至与 LoadSomeData() 方法的连接。如果您想要更正确地分离关注点,请创建一个 GetSomeData() 例程,并让 LoadSomeData() 例程调用 GetSomeData() 例程并对数据进行整形,以便可以轻松绑定数据,而不是一次消耗一行(在您的例如,您不是将数据对象绑定到下拉列表,而是一次推送一项)。

我希望这会有所帮助,但我很抱歉,许多示例在最佳实践方面确实很糟糕。是的,这包括许多来自应该更了解的人的样本。 ;-)

I would not use the Page_Load to start the connection. In .NET, the "real" connection object returns to the pool when you Dispose(), so it is there for other work. Even if you open, close and dispose numerous times, there is but a few microseconds of overhead max, so open a connection early and then keeping it open for the entire page duration does not really help you.

In addition, opening connection in Page_Load means you have tightly coupled your data access to your UI, leaving you a lot of rearchitecture and not just rearrangement to divide the work into proper tiers.

What this means is the connection should be created, used and disposed in one method (in your example). This means it is both logically separated and easy to separate into different classes and/or assemblies, should the application grow so you need a full DAL.

I would, at minimum, move all realting to connection to the LoadSomeData() method. If you want more proper separation of concerns, make a GetSomeData() routine and have the LoadSomeData() routine call the GetSomeData() routine and shape the data so it can be easily bound, rather than consume one row at a time (In your example, instead of binding a data object to the drop down, you are pushing one item at a time).

I hope this helps and I am sorry that many of the examples out there are really bad at best practices. And, yes, this includes many samples from people who should know better. ;-)

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