Iページの起動中にODBC接続を開いたり閉じたりします。どこにどこにConnection.Disposeメソッドを配置する必要がありますか?私はPage_Disposedを試しましたが、決してそこには入りません。Asp.NetでのODBC接続(接続および廃棄)
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
おそらくtry CatchのFinallyブロックに書き込んでください。 –
ユーザーがドロップダウン選択を行ったときに実際にデータベースヒットしているので、アプリケーションを閉じるまで接続を破棄したくありませんでした。または私はこれを間違って見るのだろうか? – TroyS
ここではConnectionを閉じているので、もう一度処理する必要はありません。 –