且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

线程和数据库连接。

更新时间:2023-02-02 22:50:19

检查以下内容:

1。你是否在Finally块中关闭了你的连接?

2.如果你在Finally块中处理了一些对象,检查它们是否存在或不存在;如果Not Object is Nothing Then Object.Dispose

3.抛出所有异常 - 不应该发生错误

在某个函数中并且程序向前移动。

更新命令总是在表上应用锁定,但是,

选择命令不应该,所以在所有选择查询中使用WITH NOLOCK。


Sankalp


我们遇到同样的问题

连接已经打开


即使我们对.Net很新,但我们可以设法获得解决方案。


实际上我们为所有方法创建了一个公共类在那个

构造函数中

我们已经定义了数据库的开放连接,这导致了很多问题。
>
解决方案是你要创建一个新的配置文件(即。

..config文件)。

并创建一个唯一的类,它是仅适用于

连接。


打开连接,你需要和交易完成后

,然后立即

关闭连接。当我们使用数据加载器时,这是一个很好的帮助


如果它工作正常的话,试试这个。


您好,感谢您的重播,但是,这不是我想要的答案。

错误不应该发生,所以我想知道为什么我有这个问题。

当然,把它全部放在一个尝试和捕捉程序是一个解决方法,但

这不是我的意思想要。

还有什么建议吗?


" Sankalp"写道:
检查以下内容:
1.您是否在Finally块中关闭了连接?
2.如果要在Finally块中处理某些对象,检查它们是否存在;如果Not Object is Nothing Then Object.Dispose
3.抛出所有异常 - 不应该在某个函数中发生错误并且程序继续前进。
更新命令将始终适用但是,锁定在桌面上,
选择命令不应该,所以在所有选择的查询中使用WITH NOLOCK。

Sankalp



The program works like this:
There is a form with a button.
When the form is loaded, a separate thread is started which is
retreiving/updating data in the database every x seconds.
When clicked on the button, data is retrieved from the database.

This looks to work fine. However, sometimes after clicking on the butten to
retrieve the data i got an error message (on the separate thread):
"The connection is already Open"

After changing the query that is executed when the button is clicked to a
more extensive query, the error is popping up every time.

The database is an Access database.
Each tread has it''s own connection.
Each tread opens and closes its own connection.

Who can help me?

Check the following:
1. Are you closing your connections in the Finally block?
2. If you are disposing some objects in the Finally block, check if
they exist or not; If Not Object Is Nothing Then Object.Dispose
3. Throw all your exceptions-it should not happen that an error happens
in some function and the program moves ahead.
Update commands would always apply a lock on the table, however,
select commands should not, so use WITH NOLOCK in all select queries.

Sankalp


we had the same problem of being that
"The Connection is already open"

Even we r very new to .Net but we could manage to get the solution.

Actually we created a common class for all the methods and in that
constructor only
we hav defined the open connection for the database which led to many
problems.

the solution is that u hav to create a new configuration file (ie.
..config file).
and also create the unique class which is only meant for the opening of
the connection.

and open the connection were ever u need and after the transaction
,then immediately
close the connection.this is a great help when we use datareaders

try this even for urs if it works then fine.


Hello, thank you for your replay, however, this is not the answer i was
looking for.
the error should not occur, so i would like to know why i have this problem.
Of course, putting it all in a try and catch routine is a work around, but
that is not what i want.
Any more suggestions?

"Sankalp" wrote:
Check the following:
1. Are you closing your connections in the Finally block?
2. If you are disposing some objects in the Finally block, check if
they exist or not; If Not Object Is Nothing Then Object.Dispose
3. Throw all your exceptions-it should not happen that an error happens
in some function and the program moves ahead.
Update commands would always apply a lock on the table, however,
select commands should not, so use WITH NOLOCK in all select queries.

Sankalp