在VB.Net/ADO.Net中压缩Access数据库 [英] Compact an Access database in VB.Net/ADO.Net

查看:213
本文介绍了在VB.Net/ADO.Net中压缩Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单用户VB6应用程序,我正在将其转换为VB.Net.它使用Access数据库进行存储.为了回收"自动编号,它将关闭其连接,压缩数据库,然后重新打开并继续添加记录.
我已经将所有数据库访问权限都转换为ADO.Net.现在,我已经在VB.Net中拥有了该应用程序,现在我找不到找到一种方法来强制关闭访问数据库以允许我对其进行压缩(比如说它是完全开放的).似乎关闭ADO连接不足以关闭数据库.

我在网上找不到任何解决方案.我想使该应用程序尽可能接近原始应用程序,并且关闭该应用程序不是可接受的解决方案.

请帮忙!

解决方案

我进行了一些搜索,以下内容可能会解决您的问题.

您没有提及正在使用的连接类型,而是同时 OdbcCOnnection [ ^ ]和OleDbConnection [ ReleaseObjectPool以释放ODBC环境句柄.例如,如果将不再使用连接对象,则可能要调用此方法.当环境中的所有连接都关闭时,可以处理环境.请注意,仅调用该方法实际上并不会释放环境中存在的活动连接.

在最终处置环境之前,必须发生以下情况:

1.调用Close从环境中释放OdbcConnection对象.

2.允许每个连接对象超时.

3.调用ReleaseObjectPool.

4.调用垃圾回收.

相反,如果在所有活动的连接上调用Close并调用垃圾回收,但不调用ReleaseObjectPool,则为该环境保留的资源仍然可用.

释放环境句柄后,对新OdbcConnection的请求将创建一个新环境.深入应用程序,发现一个杂散的数据库打开命令没有匹配的关闭.一旦我更正,一切正常.

感谢您的帮助!


I have a single-user VB6 application I am converting to VB.Net. It uses an Access database for storage. In order to "reclaim" autonumbers, it would close its connection, compact the database, and reopen it and continue adding records back in.

I have converted all database access to ADO.Net. Now that I have the application in VB.Net I can''t find a way to force the access database to close in order to allow me to compact it (says it is exclusively open). It seems that the closing the ADO connection is not sufficient to close the database.

I cannot find any solutions on the web for this. I want to keep the application as close to the original as possible and closing the application is not an acceptable solution.

Please help!

I did some searching and the following might solve your problem.

You do not mention which type of connection you are using, but both the
OdbcCOnnection[^] and OleDbConnection[^] have a method called ReleaseObjectPool that will help release the connection.

Follow the procedure as mentioned in the remark section of the method description:

ReleaseObjectPool can be called to release the ODBC environment handle. You might want to call this method if, for example, the connection object will not be used again. When all connections in the environment are closed, the environment can be disposed. Note that calling the method alone does not actually release the active connections that exist in the environment.

The following must occur before the environment is finally disposed:

1. Call Close to release the OdbcConnection object from the environment.

2. Allow each connection object to time out.

3. Call ReleaseObjectPool.

4. Invoke garbage collection.

Conversely, if you call Close on all active connections, and invoke garbage collection, but do not call ReleaseObjectPool, the resources reserved for the environment remain available.

After an environment handle is released, a request for a new OdbcConnection creates a new environment.


After I put this code in and saw that the database was not closing, I dug deeper into the application and found a stray database open command that did not have a matching close. Once I corrected it all works fine.

Thanks for the help!


这篇关于在VB.Net/ADO.Net中压缩Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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