関数内で開いているSQL接続を閉じる方法を教えてもらえますか?
私は次のような選択関数を呼び出します。
Function Selec(ByVal SQLStr As String) As SqlDataReader
Dim SQLConn As New SqlConnection()
Dim SQLCmd As New SqlCommand()
SQLConn.ConnectionString = Session("bd")
SQLConn.Open()
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = SQLStr
Selec = SQLCmd.ExecuteReader
End Function
そして別のページで、次のようなデータを取得するためにWhileメソッドを実行します。
(注:BDcon.BDは、関数を持つクラスの名前です)
Dim write as New BDcon.BD
Dim menu As SqlDataReader = writeBD.Selec("SELECT something from Table")
While menu.Read
'Do something
End While
menu.Close 'This close just the DataReader and not the SqlConnection
最後に、次のような関数でSQL接続を閉じます。
Function Close() As SqlConnection
Dim SQLConn As New SqlConnection()
SQLConn.ConnectionString = Session("bd")
SQLConn.Close()
End Function
問題はClose()
関数にあると思います。接続を閉じたいのですが、開いた接続を呼び出す方法がわかりません。