properly closing adodb connections in vb.net
in vb.net, there are some applications that use adodb to access a mysql server.
private adoconnect as new adodb.connection
public adors as new adodb.recordset
public function returnadors(byval column as string) as string
return adors.fields(column).value.tostring
end function
Public Function ReadData(ByVal strQuery As String, Optional ByVal strWhere As String = vbNullString) As Boolean
Try
If ADOConnect.State = ConnectionState.Open Then Call CloseConnection()
ADOConnect.Open(dsn, user, pass)
ADORS.Open(strQuery, ADOConnect, ADODB.CursorTypeEnum.adOpenDynamic)
If not ADORS.EOF Then Return True
Catch ex As Exception
msgbox(ex)
End Try
Return False
End Function
public sub closeconnection()
if adoconnect.state = connectionstate.open then adoconnect.close
end sub
now lets say we wanted to populate a textbox with something from the database:
if readdata("SELECT NAME FROM USERS WHERE ID = 1") then
me.开发者_运维技巧textbox1.text = returnadors("NAME")
end if
call closeconnection
re writing these functions is a big task - and I am not interested in doing it unless absolutely needed.
Here is my problem though.
even if the connection is closed (and i have stepped through the code, it closes) The connection is still visible on the sql server in a sleep state
why? and how can i make sure that the connection is closed.
thanks
Add the following to your ConnectionString
:
Pooling=False;
I don't have much experience with exactly what you are doing, but this is usually the path I take with sqlserver connections. Using
will automatically close your connection.
Using ADOconnect
'operations
End Using
精彩评论