F
fniles
I am using VB.NET 2003 and SQL 2005.
To use connection pooling and avoid the error "There is already an open
DataReader associated with this Connection which must be closed first." , I
understand that I want to release/close connections in a timely fashion.
What I do is I declare the SqlClient.SqlConnection variable as a local
variable inside my subroutine (instead of a global variable). Right before I
Fill the dataset I open the connection, and right after I fill the dataset I
close the connection, like shown below. Do I do it correctly ? Thank you.
sub MySub
Dim adoCon As SqlClient.SqlConnection
Dim cmdSQL As SqlClient.SqlCommand
Dim dsSQL As DataSet
Dim daSQL As SqlClient.SqlDataAdapter
adoCon = New SqlClient.SqlConnection
With adoCon
.ConnectionString = DB_Path
.Open() '--> open connection
End With
cmdSQL = New SqlClient.SqlCommand
With cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
daSQL = New SqlClient.SqlDataAdapter
dsSQL = New DataSet
daSQL.SelectCommand = m_cmdSQL
daSQL.Fill(m_dsSQL) '--> fill dataset
If Not adoCon Is Nothing Then
adoCon.Close() '--> close connection
adoCon = Nothing
End If
To use connection pooling and avoid the error "There is already an open
DataReader associated with this Connection which must be closed first." , I
understand that I want to release/close connections in a timely fashion.
What I do is I declare the SqlClient.SqlConnection variable as a local
variable inside my subroutine (instead of a global variable). Right before I
Fill the dataset I open the connection, and right after I fill the dataset I
close the connection, like shown below. Do I do it correctly ? Thank you.
sub MySub
Dim adoCon As SqlClient.SqlConnection
Dim cmdSQL As SqlClient.SqlCommand
Dim dsSQL As DataSet
Dim daSQL As SqlClient.SqlDataAdapter
adoCon = New SqlClient.SqlConnection
With adoCon
.ConnectionString = DB_Path
.Open() '--> open connection
End With
cmdSQL = New SqlClient.SqlCommand
With cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
daSQL = New SqlClient.SqlDataAdapter
dsSQL = New DataSet
daSQL.SelectCommand = m_cmdSQL
daSQL.Fill(m_dsSQL) '--> fill dataset
If Not adoCon Is Nothing Then
adoCon.Close() '--> close connection
adoCon = Nothing
End If