i wrote a .NET function :
Public Function ExecuteSql(strSql As String, connectionString As String) As SqlDataReader
Dim conn As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(strSql, conn)
Dim dr As SqlDataReader
Try
conn.Open()
dr = cmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
Return dr
End Function
As you can see, this function return a datareader, so i can manipulate data as i like.
The problem is: the connection remain open.
How to solve this problem ? Is there a command to ‘close globally connection’ ?
Thanks
You have to call
after you are done working with the data reader. Alternatively, you can create a DataAdapter, and then use Fill to fill a DataTable. When using a datatable, you can close the connection right away, as soon as you call fill, and you don’t have to wait until the code on the other side of your “ExecuteSQL” function is done as you do with the DataReader.