ggyyppmm

遨游于博大精深的.net

 

应用程序中oracle数据库备份和恢复

昨天花了一个多小时写的一个小东西,可能以前有很多人写过了,不过还是放上来看看

备份,可以实现在应用程序文件夹内生成一个DataBaseBak文件夹,并把dmp文件按照时间来保存在这个文件夹内,我设的精度是分钟,可以自行更改,异常写入系统日志:

   string startpath = Application.StartupPath;
   DirectoryInfo di = new DirectoryInfo(startpath+"\\DataBaseBak");
   di.Create();
   string oraPath = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Oracle").GetValue("ORACLE_HOME").ToString();
   System.Diagnostics.Process proc = new System.Diagnostics.Process();
   proc.EnableRaisingEvents = false;
   proc.StartInfo.FileName = oraPath +"\\bin\\EXP.exe";
   proc.StartInfo.Arguments = " username/password@servicename file="+di.FullName+"\\filename"+DateTime.Now.ToString("yyyy-MM-dd-HH-mm")+".dmp owner=username";
   proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
   this.Cursor = Cursors.WaitCursor;
   try
   {
    proc.Start();
    MessageBox.Show("数据库备份成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
   }
   catch(Exception ee)
   {
    MessageBox.Show("数据库备份失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    EventLog newLog=new EventLog();
    newLog.Source = "OracleErr";
    newLog.WriteEntry(ee.Message,System.Diagnostics.EventLogEntryType.Error);
   }
   finally
   {
    proc.Dispose();
   }

   this.Cursor = Cursors.Arrow;

恢复,可以自动定位到DataBaseBak文件夹,然后自行选择需要的dmp文件,进行恢复,并将异常写入系统日志:

OpenFileDialog selectDMPDialog = new OpenFileDialog();
   selectDMPDialog.InitialDirectory = Application.StartupPath + "\\DataBaseBak";
   selectDMPDialog.Filter = "备份文件(*.dmp)|*.dmp";
   if(selectDMPDialog.ShowDialog() == DialogResult.OK)
   {
    string oraPath = Registry.LocalMachine.OpenSubKey("software").OpenSubKey("Oracle").GetValue("ORACLE_HOME").ToString();
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.EnableRaisingEvents = false;
    proc.StartInfo.FileName = oraPath+"\\bin\\IMP.exe";
    proc.StartInfo.Arguments = " username/password@servicename file="+selectDMPDialog.InitialDirectory+"\\"+selectDMPDialog.FileName+" fromuser=username";
    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    this.Cursor = Cursors.WaitCursor;
    try
    {
     proc.Start();
     MessageBox.Show("数据库导入成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    catch(Exception ee)
    {
     MessageBox.Show("数据库备份失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
     EventLog newLog=new EventLog();
     newLog.Source = "OracleErr";
     newLog.WriteEntry(ee.Message,System.Diagnostics.EventLogEntryType.Error);
    }
    finally
    {
     proc.Dispose();
    }
    this.Cursor = Cursors.Arrow;

posted on 2004-09-02 12:00  .netbc  阅读(2186)  评论(2编辑  收藏  举报

导航