C# 如何删除 Access Db 的临时 ldb 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1015336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:36:13  来源:igfitidea点击:

How Can I Remove Access Db's temporary ldb file

c#asp.netms-accessado.netjet

提问by Ibrahim AKGUN

I have an ASP.NET Project. I am connecting to the DB and then closing and disposing of the connection object. But when anyone enters my site, MS Access creates a temporary dbname.ldb. When I want to download my original mdb file from my server it won't let me access the mdb file. I can't do anything if there is ldb file in server. It's locking mdb file and I can't move it. So what's the problem? I am opening connection and closing it. So why this ldb file not deleting itself after the connection is closed.

我有一个 ASP.NET 项目。我正在连接到数据库,然后关闭并处理连接对象。但是当有人进入我的站点时,MS Access 会创建一个临时的 dbname.ldb。当我想从我的服务器下载我的原始 mdb 文件时,它不会让我访问 mdb 文件。如果服务器中有 ldb 文件,我将无能为力。它正在锁定 mdb 文件,我无法移动它。所以有什么问题?我正在打开连接并关闭它。那么为什么这个 ldb 文件在连接关闭后不会自行删除。

采纳答案by backslash17

The connection can be left open if the scripts produces an error of any kind before to close it. Check the scripts with a customized error 500 page that logs in a text file the errors and you will see if this is the case. Anyway the ldb file is not dangerous so you can create a script to remove them once a day or so. This is one of the drawbacks about working web applications with MS Access. Try to migrate to MSSQL if you can or to MySQL, this last can be used from .NET or classic ASP with no problem with ADO or ADO.NET with the appropiate driver.

如果脚本在关闭连接之前产生任何类型的错误,则连接可以保持打开状态。检查带有自定义错误 500 页面的脚本,该页面将错误记录到文本文件中,您将看到是否是这种情况。无论如何,ldb 文件并不危险,因此您可以创建一个脚本来每天删除一次。这是使用 MS Access 工作 Web 应用程序的缺点之一。如果可以,请尝试迁移到 MSSQL 或 MySQL,这最后一个可以从 .NET 或经典 ASP 使用,而 ADO 或 ADO.NET 与适当的驱动程序没有问题。

回答by Renaud Bompuis

The .ldbfile is the lock file for .mdbAccess databases. Every time you open a database, the Jet engine will create the lock file and keep it open for as long as someone is connected. Once there are no other clients connected to the database, Jet removes the lock file.

.ldb文件是.mdbAccess 数据库的锁定文件。每次打开数据库时,Jet 引擎都会创建锁定文件并在有人连接时将其保持打开状态。一旦没有其他客户端连接到数据库,Jet 将删除锁定文件。

So you are seeing the lock file for one of two reasons:

因此,由于以下两个原因之一,您会看到锁定文件:

  • There is an open connection.
  • There is a broken connection, and the lock file could not be removed.
  • 有一个开放的连接。
  • 连接断开,无法删除锁定文件。

If checking the error logs of your server doesn't give you anything, try to log all database accesses from within your application to a file: append information about the time, connection and other useful debug information.
That could be an easy way to quickly debug the problem and see where and when the connection is left open.

如果检查服务器的错误日志没有给您任何信息,请尝试将应用程序内的所有数据库访问记录到文件中:附加有关时间、连接和其他有用调试信息的信息。
这可能是一种快速调试问题并查看连接在何时何地保持打开状态的简单方法。

Alternatively, you could have a look at this CodeProject article: Find "Leaked" Database Connections in ASP.NET Web Applications.

或者,您可以查看这篇 CodeProject 文章:在 ASP.NET Web 应用程序中查找“泄露的”数据库连接

回答by Chris

You also may want to consider connection pooling - when you close a connection, c# actually keeps it open for a while (30 secs? 60 secs?) before reallyclosing it, in case it can be re-used. This may be an issue.

您可能还想考虑连接池 - 当您关闭连接时,c# 实际上会在真正关闭它之前将其打开一段时间(30 秒?60 秒?),以防它可以重新使用。这可能是一个问题。

回答by Cade Roux

Your web application within IIS is keeping the connection open with connection pooling. The IIS application will eventually close if there are no further connections within the time IIS is set to terminate your web application or you can restart the application (and copy the file before anyone gets in).

IIS 中的 Web 应用程序通过连接池保持连接打开。如果在 IIS 设置为终止您的 Web 应用程序的时间内没有进一步的连接,则 IIS 应用程序最终将关闭,或者您可以重新启动应用程序(并在任何人进入之前复制文件)。

That's just one reason Access is not a good choice of database for this kind of application.

这只是 Access 不是此类应用程序的良好数据库选择的一个原因。

回答by Spike

One hacky workaround to remove the .ldb file is to do the following:

删除 .ldb 文件的一种变通方法是执行以下操作:

  • Make a copy of the access database
  • Delete the original access database
  • Delete the .ldb file
  • Rename the copy of the access database to the name of the original one.
  • 制作 access 数据库的副本
  • 删除原来的access数据库
  • 删除 .ldb 文件
  • 将 access 数据库的副本重命名为原始数据库的名称。

回答by Gustavo Chacón Bustos

You need to Call GC.Collect()after Close and Dispose the Connection Object :)

您需要在关闭并处理连接对象后调用GC.Collect():)