正确关闭 vb.net 中的 adodb 连接
在vb.net中,有一些应用程序使用adodb来访问mysql服务器。
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
现在假设我们想用数据库中的内容填充文本框:
if readdata("SELECT NAME FROM USERS WHERE ID = 1") then
me.textbox1.text = returnadors("NAME")
end if
call closeconnection
重新编写这些函数是一项艰巨的任务 - 除非绝对需要,否则我对这样做不感兴趣。
这是我的问题。
即使连接关闭(并且我已单步执行代码,它也会关闭) 连接在处于睡眠状态的sql server上仍然可见,
为什么?我怎样才能确保连接已关闭。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将以下内容添加到您的
ConnectionString
中:Add the following to your
ConnectionString
:我对你正在做的事情没有太多经验,但这通常是我使用 sqlserver 连接所采取的路径。
使用
将自动关闭您的连接。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.