从数据库和文件系统删除文件 [英] Delete a file from database and file system

查看:130
本文介绍了从数据库和文件系统删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有引用我们的网络(存储在数据库中的文件路径)上的共享位置中的文件的表。



我有需要删除一个按钮从数据库中的记录和文件关闭文件系统:

 的foreach(在报告中VAR报告)
{
字符串文件路径= report.ReportPath;

如果(File.Exists(文件路径));
{
File.Delete(文件路径);
}

context.ReportGenerations.DeleteObject(报告);
context.SaveChanges();
}

这是例外,可以删除文件或删除数据库记录时,被抛出,如果出现这种情况,我想无论是操作来完成。



有一种简单的方法,以确保这两个操作都进行了成功?


< DIV CLASS =h2_lin>解决方案

您必须做2两件事情




  • 包住一个数据库事务内部全过程。


  • 在删除数据库文件中的 从文件系统做




如果该进程失败,从数据库中删除,物理文件不会因为你没有达到文件系统中删除的逻辑。删除



如果该进程失败,从文件系统中删除您回滚事务,数据库操作恢复。

  DbTransaction交易= NULL; 
的foreach(在报告中VAR报告)
{

{
=交易context.Connection.BeginTransaction();

context.ReportGenerations.DeleteObject(报告);
context.SaveChanges();

字符串文件路径= report.ReportPath;
如果(File.Exists(文件路径));
{
File.Delete(文件路径);
}
器transaction.commit();
}

{
transaction.Rollback();
}
}





虽然我相信这是可以实现wihout变得非常复杂我同意是保证疗效的100%,没有同步的方法更安全的方法。为了确保没有孤儿项目仍然存在,你必须实现一个后台清理过程。您将有如果这种额外的复杂性是有道理的,或不按你的情况来分析。


I have a table that references files in a shared location on our network (stores the file path in the database).

I have a button that needs to delete the record from the database and the file off the File System:

foreach (var report in reports)
{
      string filePath = report.ReportPath;

      if (File.Exists(filePath));
      {
         File.Delete(filePath);
      }                      

      context.ReportGenerations.DeleteObject(report);
      context.SaveChanges();
}

An exception could be thrown when deleting the file or deleting the database record and if this happens I would like neither of the operations to complete.

Is there an easy way to ensure both operations are carried out successfully?

解决方案

You have to do 2 two things

  • Wrap the whole process inside a database transaction.

  • Delete file from database before doing it from file system

If the process fails deleting from database, physical file won't be removed as you haven't reached file system delete logic.

If the process fails deleting from file system you rollback transaction and database operation is reverted.

DbTransaction transaction = null;
foreach (var report in reports)
{
    try
    {
        transaction = context.Connection.BeginTransaction();

        context.ReportGenerations.DeleteObject(report);
        context.SaveChanges();

        string filePath = report.ReportPath;
        if (File.Exists(filePath));
        {
            File.Delete(filePath);
        }
        transaction.Commit();
    }
    catch
    {
        transaction.Rollback();
    }
}        


While I believe this is the safer approach you can implement wihout getting really complex I agree that there is no synchronous approach that guarantee 100% of efficacy. To be sure that no orphan item remains, you'll have to implement a background clean up process. You'll have to analyze if such an extra complexity is justified or not according your scenario.

这篇关于从数据库和文件系统删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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