関数内で開いているSQL接続をどのように閉じることができますか?ASP.NET(VB) - 関数内で開いている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
そして、私は私にこのようなデータを取得するために、一方でメソッドを実行する別のページで:
(注: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()
にあると思います。機能は、接続を終了したいのですが、開かれたコネネットを呼び出す方法はわかりません。
あなたのために働いていますか? –