简体   繁体   English

尝试删除数据库文件时“另一个进程正在使用的文件”错误

[英]“file in use by another process” error while trying to delete a database file

I have a PC.sdf file that I work with. 我有一个我使用的PC.sdf文件。 I close the connection and I need to delete it. 我关闭连接,我需要删除它。

I open the connection like this: 我这样打开连接:

bool OpenConn()
{
  try
  {
     Conn = new SqlCeConnection(String.Format(@"Data Source={0}\{1}", PathI, "PC.SDF"));
     Conn.Open();
     return true;
   }
   catch 
   {
      //MessageBox.Show(err.Message, "Connetion error");
      return false;
   }
}

I close it like this: 我这样关闭它:

Conn.Close();
Conn.Dispose();

I try to delete it like this: 我试着像这样删除它:

if (File.Exists(@"\myPath\PC.sdf"))
    File.Delete(@"\myPath\PC.sdf");

But I get this error: file in use by another process . 但我收到此错误: file in use by another processfile in use by another process What could be the cause of this error, and how can I fix it? 可能是导致此错误的原因,我该如何解决?

You could try and force garbage collection by running 您可以尝试通过运行强制垃圾收集

GC.Collect();

Do this after you have Closed and Disposed of the DB object. 在关闭和处置DB对象后执行此操作。

This will of course only work if that is the only reference to that database file. 这当然只有在那是对该数据库文件的唯一引用时才有效。

Edit: Answer to comment about that you shouldn't use GC.Collect to "fix" other issues. 编辑:回答评论你不应该使用GC.Collect来“修复”其他问题。

I don't think this is because of another issue. 我不认为这是因为另一个问题。 The garbage collector runs at an indeterminate time chosen by the runtime. 垃圾收集器在运行时选择的不确定时间运行。 This means that you can't rely on your object being disposed of between the followings lines. 这意味着您不能依赖于在以下行之间处理您的对象。

Conn.Dispose();
//Nothing says GC will run exactly now
File.Delete(@"C:\Some-file-used-by-Conn.db");

Your options are to force garbage collection or to delay the deletion in some way. 您的选择是强制垃圾收集或以某种方式延迟删除。

I would try it like this below. 我会在下面尝试这样的。 This puts your connection inside a using block which will call dispose the connection for you once it goes out of scope. 这会将您的连接放在一个使用块中,一旦它超出范围,它将调用为您配置连接。 This should allow you to delete the file immediately after as all of the file locks associated with it should be released. 这应该允许您立即删除该文件,因为应该释放与之关联的所有文件锁。

bool OpenConn()
{
  try
  {
     using(Conn = new SqlCeConnection(String.Format(@"Data Source={0}\{1}", PathI, "PC.SDF")))
     {
        Conn.Open();
        return true;
     }
   }
   catch 
   {
      //MessageBox.Show(err.Message, "Connetion error");
      return false;
   }
}

if (File.Exists(@"\myPath\PC.sdf"))
    File.Delete(@"\myPath\PC.sdf");

The two responses above are both good considerations for ensuring that it is not YOUR application which is the one using the database file. 上面的两个响应都是确保它不是您使用数据库文件的应用程序的良好考虑因素。

I'm not exactly sure what an .SDF file is, but a quick google search suggests that it may be a MS SQL database file. 我不确定.SDF文件是什么,但快速谷歌搜索表明它可能是MS SQL数据库文件。 If so, depending on your system and your operating conditions, you may want to consider using the ALTER DATABASE SET SINGLE_USER command: 如果是这样,您可能需要考虑使用ALTER DATABASE SET SINGLE_USER命令,具体取决于您的系统和操作条件:

ALTER DATABASE [YourDbName]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

http://blog.sqlauthority.com/2010/02/11/sql-server-alter-database-dbname-set-single_user-with-rollback-immediate/ http://blog.sqlauthority.com/2010/02/11/sql-server-alter-database-dbname-set-single_user-with-rollback-immediate/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在while循环中删除文件时出现错误,被另一个进程错误使用 - Error in Delete a file in a while loop with being used by another process error 尝试删除 XML 文件会引发“进程无法访问文件...”错误 - Trying to delete an XML file throws “The process cannot access the file…” error 该进程无法访问该文件,因为尝试写入文件时出错 - The process cannot access the file because error while trying to write to a file 如何修复“文件正在被另一个进程使用”错误 - How to fix 'file is in use by another process' error “尝试删除文件时,进程无法访问该文件,因为它正被另一个进程使用” - “the process cannot access the file because it is being used by another process” when trying to delete file 如何删除其他进程正在使用的文件? - How can I delete a file that is in use by another process? File.Delete错误“该进程无法访问该文件,因为该文件正在被另一个进程使用” - File.Delete error “The process cannot access the file because it is being used by another process” 当文件被另一个进程使用时,StorageFile读取内容 - StorageFile read content while file is in use by another process 文件正在被另一个进程使用…虽然我可以复制 - file in use by another process … while i can copy 错误:在openxml中生成文档时“文件被另一个进程使用” - error:“File is used by another process” while generating document in openxml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM