0

関数内で開いている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()関数にあると思います。接続を閉じたいのですが、開いた接続を呼び出す方法がわかりません。

4

5 に答える 5

1

consructを使用する方が良いと思います...

using cn as new system.data.sqlclient.sqlconnection()
    cn.open
    '{do a bunch of other stuff with commands and datareaders here}
    cn.close 
end using 

また

あなたも利用することができますCommandBehavior Enumeration

このために読んでください:CommanBehaviorでExecuteReader(データを読んだ後に自動的に接続を閉じます)

CloseConnection-コマンドが実行されると、関連付けられたDataReaderオブジェクトが閉じられると、関連付けられたConnectionオブジェクトが閉じられます。

于 2012-04-11T11:21:27.587 に答える
0

私の意見では、関数を変更してDataTableを返し、すぐに接続を閉じる必要があります。
例としては

Function Select(ByVal SQLStr As String) As DataTable
    Dim SQLConn As New SqlConnection(Session("bd"))
    Dim SQLCmd As New SqlCommand(SQLStr, SQLConn)
    Dim SQLAdapt As New SqlDataAdapter(SQLCmd)
    Dim SQLDt As New DataTable()
    SQLAdapt.Fill(SQLDt)
    SQLConn.Close()
    Return SQLDt
End Function

またはGetConnection、SQLConnectionを作成して返す関数を作成し、これをSelect関数に渡して(内部に新しい関数を作成せずに)、手動で接続を閉じることもできます。

于 2012-04-11T11:24:17.750 に答える
0

以前に作成したClose関数の引数としてSqlConnectionクラスのオブジェクトを渡す必要があると思います。そして、そのオブジェクトをその関数で使用します。

于 2012-04-11T11:24:36.100 に答える
0

元のコードでは、接続オブジェクトのスコープはそれを作成した関数のみであるため、元の接続を閉じることはできません(作成した関数内からのみアクセスできます)。マルコの答えに同意します。SQLDataReaderではなくDataTableを返す必要があります。これは、SQLDataAdapterを必要としないわずかに異なるアプローチです。

Function Selec(ByVal SQLStr As String) As DataTable
    Dim SQLConn As New SqlConnection(Session("bd"))
    Dim SQLCmd As New SqlCommand(SQLStr, SQLConn)
    Dim SQLDt As New DataTable()
    SQLConn.Open()
    SQLDt.Load(SQLCmd.ExecuteReader)
    'Close the connection as soon as it is no longer needed
    SQLConn.Close() 
    Return SQLDt
End Function

返されたテーブルを使用するには、次のようにDataRowsのコレクションをウォークスルーできます。

Sub DoSomthing()
    Dim menu As DataTable = Selec("SELECT * FROM SomeTable")
    For Each row As DataRow In menu.Rows
        ' do something
    Next
End Sub

別の方法は、返されたDataTableの上にDataTableReaderを作成することですが、元のコードに似ていることを除いて、そうすることの利点はわかりません。

Sub AlternateDoSomething()
    Dim dt As DataTable = Selec("SELECT * FROM SomeTable")
    Dim menu As DataTableReader = dt.CreateDataReader
    While menu.Read
        'Do something
    End While
    menu.Close()
End Sub
于 2012-04-11T13:12:24.787 に答える
0

PranayRanaの返信で問題を解決しました。

私のコードにCommandBehavior.CloseConnectionを追加しました。

Selec関数の最終コード:

   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(CommandBehavior.CloseConnection)

    End Function

もう一度ありがとう!:D

于 2012-04-12T12:24:26.067 に答える