sqlconnection 连接数的问题请教?基本问题请教?
我在公共类中定义了连接数据库函数.代码如下:
Public Function OpenConnect() As SqlConnection
Try
Dim connectionString As String = _
"Persist Security Info=False;User ID=test;Password=test;Initial Catalog=test;Data Source=" & DataServer & ";"
Dim Connect As New SqlConnection(connectionString)
If Connect.State <> ConnectionState.Open Then
Connect.Open()
End If
Return Connect
Catch ex As Exception
MsgBox("Can't connect to the dataserver!", MsgBoxStyle.Critical, "Wrong")
End Try
Return Nothing
End Function
我在应用类是如下样式使用上面的连接;
dim conn As New SqlConnection
Dim Cmd As New SqlCommand
Cmd.Connection = conn
''Cmd.Connection = OpenConnect()
Cmd.CommandText = "select ID,Name from sysProcess where Line_ID='A' "
Dim DataReader As SqlDataReader = Cmd.ExecuteReader
ComboBox1.Items.Clear()
Do While DataReader.Read
ComboBox1.Items.Add(DataReader(0) & ":" & DataReader(1))
ComboBox3.Items.Add(DataReader(2) & ":" & DataReader(3))
Loop
DataReader.Close()
Cmd.Dispose()
conn.Dispose()
我是打开连接后,我又用conn.Dispose() 释放连接啦..
我直接用OpenConnect.Dispose()释放也试过.
但我在数据库后台查询的时候,sql server 2008 显示我的程序还有两个连接连在数据库上?
sql server 2008 查询连接数量
select host_name,program_name,count(*) as sl
from sys.dm_exec_sessions
group by host_name,program_name
order by sl desc
请问是什么原因?该怎么来释放连接?
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- conn.Dispose()改为 conn.close() --------------------编程问答--------------------
谢谢楼上回答。
用conn.close后,效果还是一样,在服务器后台查询sys.dm_exec_sessions 中连接数还是一样没有关闭..
请指点... --------------------编程问答-------------------- 试试Cmd.Connection.close
你的conn和Cmd.Connection已经分离了!
补充:.NET技术 , VB.NET