关闭SQL连接 [英] Closing SQL Connections

查看:88
本文介绍了关闭SQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人可以帮忙。


我有一个例程如下


公共函数dbConnection()As SqlConnection


Dim dbConn As New SqlConnection

Dim errorH As New errorHandle


试试

试试

dbConn.ConnectionString =

ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString

Catch ex As Exception

errorH.move(ex)

结束尝试

如果dbConn.State = ConnectionState.Closed那么

dbConn.Open()

结束如果

Catch ex As SqlException

errorH.move(ex)

结束尝试


返回dbConn

结束函数


这会创建我的sqlconnection。我用这种格式从我的

类中的其他函数调用这个格式


尝试

dim sqlconn as sqlconnection = dbconnection()


Catch ex as exception


最后

如果db.State = ConnectionState.Open那么

db.Close()

dbConnection.Dispose()

结束如果

结束尝试


我最近只添加了dbconnection.dispose,因为我读到了我应该做的不确定我是否应该这样做。但我得到了通常的

错误消息,已达到最大超时或已达到最大

池大小。我不知道该怎么办。任何人都可以提供建议。

Please can someone help.

I have a routine as follows

Public Function dbConnection() As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

Try
Try
dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
End Try
If dbConn.State = ConnectionState.Closed Then
dbConn.Open()
End If
Catch ex As SqlException
errorH.move(ex)
End Try

Return dbConn
End Function

This creates my sqlconnection. I call this from other functions in my
class in this format

Try
dim sqlconn as sqlconnection = dbconnection()

Catch ex as exception

Finally
If db.State = ConnectionState.Open Then
db.Close()
dbConnection.Dispose()
End If
End try

I recently only added the dbconnection.dispose because i read that i
should be doing that not sure if i should. But i''m getting the usual
error message of the maximum timeout has been reached or the maximum
pool size has been reached. I don''t know what to do. Can anyone advise.

推荐答案

我不知道你要关闭什么,但这不是你宣布的连接


您需要在Try块之外声明变量,但在其中获取

连接。类似的东西:


Dim sqlconn as SqlConnection

尝试

sqlconn = dbconnection()

Catch


最后

如果不是IsNothing(sqlconn)那么

sqlconn.Close()

结束如果

结束尝试

" bannaman" < JK **** @ f2s.com>在消息中写道

news:11 ********************** @ j33g2000cwa.googlegr oups.com ...
I don''t know what you are closing, but it is not the connection your declare
in your Try block.

You need to declare the variable outside the Try block, but get the
connection in it. Something like:

Dim sqlconn as SqlConnection
Try
sqlconn = dbconnection()
Catch

Finally
If Not IsNothing(sqlconn) Then
sqlconn.Close()
End If
End Try
"bannaman" <jk****@f2s.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
请有人帮忙。

我有一个例程如下

公共函数dbConnection()As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

尝试
尝试dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
结束尝试
如果dbConn.State = ConnectionState.Closed那么
dbConn.Open()
结束如果
Catch ex As SqlException
errorH.move(ex)
结束尝试

返回dbConn

结束功能
<这创建了我的sqlconnection。我用这种格式从我的
类中的其他函数调用它

dim sqlconn作为sqlconnection = dbconnection()

Catch ex as exception

最后
如果db.State = ConnectionState.Open那么
db.Close()
dbConnection.Dispose()
结束如果
结束试试

我最近只添加了dbconnection.dispose,因为我读到我应该这样做,不确定我是否应该这样做。但是我得到了通常的
错误消息,已达到最大超时或已达到最大游泳池大小。我不知道该怎么办。任何人都可以提供建议。
Please can someone help.

I have a routine as follows

Public Function dbConnection() As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

Try
Try
dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
End Try
If dbConn.State = ConnectionState.Closed Then
dbConn.Open()
End If
Catch ex As SqlException
errorH.move(ex)
End Try

Return dbConn
End Function

This creates my sqlconnection. I call this from other functions in my
class in this format

Try
dim sqlconn as sqlconnection = dbconnection()

Catch ex as exception

Finally
If db.State = ConnectionState.Open Then
db.Close()
dbConnection.Dispose()
End If
End try

I recently only added the dbconnection.dispose because i read that i
should be doing that not sure if i should. But i''m getting the usual
error message of the maximum timeout has been reached or the maximum
pool size has been reached. I don''t know what to do. Can anyone advise.



如果你打电话关闭,你不需要打电话处理。


- 布鲁斯(sqlwork.com)

" bannaman" < JK **** @ f2s.com>在消息中写道

news:11 ********************** @ j33g2000cwa.googlegr oups.com ...
if you call close you do not need to call dispose.

-- bruce (sqlwork.com)
"bannaman" <jk****@f2s.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
请有人帮忙。

我有一个例程如下

公共函数dbConnection()As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

尝试
尝试dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
结束尝试
如果dbConn.State = ConnectionState.Closed那么
dbConn.Open()
结束如果
Catch ex As SqlException
errorH.move(ex)
结束尝试

返回dbConn

结束功能
<这创建了我的sqlconnection。我用这种格式从我的
类中的其他函数调用它

dim sqlconn作为sqlconnection = dbconnection()

Catch ex as exception

最后
如果db.State = ConnectionState.Open那么
db.Close()
dbConnection.Dispose()
结束如果
结束试试

我最近只添加了dbconnection.dispose,因为我读到我应该这样做,不确定我是否应该这样做。但是我得到了通常的
错误消息,已达到最大超时或已达到最大游泳池大小。我不知道该怎么办。任何人都可以建议。
Please can someone help.

I have a routine as follows

Public Function dbConnection() As SqlConnection

Dim dbConn As New SqlConnection
Dim errorH As New errorHandle

Try
Try
dbConn.ConnectionString =
ConfigurationManager.ConnectionStrings(globalapp.c urrentSite).ConnectionString.ToString
Catch ex As Exception
errorH.move(ex)
End Try
If dbConn.State = ConnectionState.Closed Then
dbConn.Open()
End If
Catch ex As SqlException
errorH.move(ex)
End Try

Return dbConn
End Function

This creates my sqlconnection. I call this from other functions in my
class in this format

Try
dim sqlconn as sqlconnection = dbconnection()

Catch ex as exception

Finally
If db.State = ConnectionState.Open Then
db.Close()
dbConnection.Dispose()
End If
End try

I recently only added the dbconnection.dispose because i read that i
should be doing that not sure if i should. But i''m getting the usual
error message of the maximum timeout has been reached or the maximum
pool size has been reached. I don''t know what to do. Can anyone advise.



抱歉昏暗的sqlconn为sqlconnection = dbconnection()是一个错字它

应该是昏暗的数据库作为sqlconnection = dbconnection()。


因为我在另一个函数中创建连接并将其传回

到我的例程它是否仍然关闭连接

调用dbconnection时打开了。我不需要做dbconnection.close或将

db.close足够吗?

Sorry the dim sqlconn as sqlconnection = dbconnection() was a typo it
should have been dim db as sqlconnection = dbconnection().

As i am creating a connection in another function and passing it back
to my routine does it still close the conneciton that was opened when
calling dbconnection. do i not need to do dbconnection.close or will
db.close be enough?


这篇关于关闭SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆