昨天花了一個(gè)多小時(shí)寫的一個(gè)小東西,可能以前有很多人寫過(guò)了,不過(guò)還是放上來(lái)看看
備份,可以實(shí)現(xiàn)在應(yīng)用程序文件夾內(nèi)生成一個(gè)DataBaseBak文件夾,并把dmp文件按照時(shí)間來(lái)保存在這個(gè)文件夾內(nèi),我設(shè)的精度是分鐘,可以自行更改,異常寫入系統(tǒng)日志:
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("數(shù)據(jù)庫(kù)備份成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception ee)
{
MessageBox.Show("數(shù)據(jù)庫(kù)備份失敗","提示",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;
恢復(fù),可以自動(dòng)定位到DataBaseBak文件夾,然后自行選擇需要的dmp文件,進(jìn)行恢復(fù),并將異常寫入系統(tǒng)日志:
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("數(shù)據(jù)庫(kù)導(dǎo)入成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception ee)
{
MessageBox.Show("數(shù)據(jù)庫(kù)備份失敗","提示",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;