[原创]C# 实例Oracle 备份,带进度提示

使用C#调用Exp 来执行 Oracle 的备份。前辈们也讲过许多,我也再整理一下我的思路及备份进度提示的处理思路

总体思路

  1. 判断目录存在性,处理。
  2. 判断最小剩余空间。
  3. 导出准备:设置导出文件名。
  4. 导出准备:构造CMD 命令串。
  5. 导出准备:取表空间已使用大小,预估导出后文件大小。
  6. 启动导出文件大小监视线程。
  7. 执行命令。
  8. 检查文件是否存在,成功失败判断。
取得表空间已使用大小 -
/// <summary>
/// 取得表空间已使用大小
/// </summary>
/// <param name="tableSpaceName">表空间名称</param>
/// <returns>已使用大小</returns>
public double GetDBUsedSize(string tableSpaceName)
{
    string sql = string.Format("select " +
        " (a.bytes-b.bytes)/1024/1024 \"UsedMB\" " +
        " from " +
        "(select tablespace_name,sum(bytes) bytes from dba_data_files " + 
        " group by tablespace_name) a, " +
        " (select tablespace_name,sum(bytes) bytes,max(bytes) largest from " +
        "dba_free_space group by tablespace_name) b " +
        " where a.tablespace_name=b.tablespace_name AND A.TABLESPACE_NAME='{0}'",
        tableSpaceName);

    object res = dboCenter.ExecuteSingle(sql);

    if (Convert.IsDBNull(res))
    {
        return 0D;
    }
    else
    {
        return Convert.ToDouble(res);
    }
}

监视文件大小的线程

exportedSize = (int)(tableSpaceUsedMB * 0.8);//预估压缩率为 80%;
if (ActReadToBack != null)
{
    ActReadToBack(exportedSize);

    if (ActBackProcessing != null)
    {
        tmMonitorProcess = new System.Threading.Timer(
            new System.Threading.TimerCallback
                (o =>
                {
                    ActBackProcessing(GetFileSize(fileNameForExport));
                }),
                null, 0, 1000
         );
    }
}