最近公司的oracle備份工具不好使了,原來是公司的人用VB寫的,由於我是主攻C#的,所以想着自己來寫一個C#版本的oracle備份和還原工具。
一開始,我按照原來的設計思路來進行編寫,想在plussql或者cmd中測試好備份的sql語句,然后開始編寫程序。
網上說了一大推關於C#備份oracle的例子,但大多數我覺得都不好使,因為本人測試了很多,都是渣渣~~~~~(雖然我水平也不高。。)
網上說的不外乎用C#來調用windows自帶的cmd程序來進行,但是會出現一個問題,就是C#調用cmd程序后,你會發現,怎么樣都寫不進參數,也看不到結果,而且處理不好,經常會出現“假死”的現象,我問過公司的同事,他們建議我寫好批處理,然后調用cmd,執行(最終證明不可行,因為批處理還是要調用cmd程序)
無意中,發現,原來oracle自帶的就有備份和還原的工具。如果你安裝了oracle的服務端的話,你可以查看目錄文件下面的BIN文件下,有兩個exe程序,一個是exp.exe,另一個是imp.exe這兩個就是備份和還原的工具。手工調用cmd備份時,其實也是調用的這個程序,於是,為何不在C#直接調用呢。
經過一番測試,終於搞出來了~~~
好了,上實例:
//調用oracle exp.exe來備份數據 private void OracleBackUp() { string tables = ""; string sql = "select * from all_users where username like '%_" + LocalMark.Text.Trim().ToUpper() + "'"; using (OracleCommand oracom = new OracleCommand(sql, orclConnection())) { oracom.Connection.Open(); using (OracleDataReader reader = oracom.ExecuteReader()) { while (reader.Read()) { tables += string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("username"))) ? "" : reader.GetString(reader.GetOrdinal("username")).ToUpper() + ","; } } } tables = tables.Substring(0, tables.Length - 1); //記錄備份的所有方案名到一個txt中,為后面的還原做准備 string paths = ExpPath.Text + "\\" + System.DateTime.Today.ToString("yyyyMMdd") + ".txt"; if (!File.Exists(ExpPath.Text +"\\"+ System.DateTime.Today.ToString("yyyyMMdd") + ".txt")) { FileInfo myfile = new FileInfo(ExpPath.Text+"\\"+ System.DateTime.Today.ToString("yyyyMMdd") + ".txt"); FileStream fs = myfile.Create(); fs.Close(); } FileStream afile = new FileStream(ExpPath.Text + "\\" + System.DateTime.Today.ToString("yyyyMMdd") + ".txt", FileMode.OpenOrCreate); StreamWriter sm = new StreamWriter(afile); String[]tabArr=tables.Split(','); foreach(string s in tabArr) { sm.WriteLine(s); sm.Flush();//清除緩沖區 } sm.Close(); //創建一個進程實例 Process p = new Process(); //生成備份文件的文件名稱 string filename = ExpPath.Text+"\\" + System.DateTime.Today.ToString("yyyyMMdd") + ".dmp"; string logname = ExpPath.Text+"\\" + System.DateTime.Today.ToString("yyyyMMdd") + ".log"; //導出程序路徑 p.StartInfo.FileName = "D:\\app\\loracle\\product\\11.2.0\\dbhome_1\\BIN\\exp.exe"; //啟用操作系統外殼程序執行 p.StartInfo.UseShellExecute = true; //顯示dos窗口執行過程 p.StartInfo.CreateNoWindow = false; //執行參數用戶名和密碼還有本機配置的Oracle服務名[kdtc/bjdscoal@tns:orcl file=" + filename + ] // p.StartInfo.Arguments = "system/system@orcl file=" + filename+" owner=HYMGS_SYSINFO_NULL"; p.StartInfo.Arguments = "system/system@orcl file=" + filename + " owner=(" + tables+") log="+logname; p.Start(); p.Dispose(); } //獲取要備份的表名 public string GetTables() { string tables = ""; string sql = "select * from all_users where username like '%_" + LocalMark.Text.Trim().ToUpper() + "'"; using (OracleCommand oracom = new OracleCommand(sql, orclConnection())) { oracom.Connection.Open(); using (OracleDataReader reader = oracom.ExecuteReader()) { while (reader.Read()) { tables += string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("username"))) ? "" : reader.GetString(reader.GetOrdinal("username")).ToUpper()+","; } } } tables = tables.Substring(0, tables.Length - 1); return tables; }
//查詢所有后綴為指定后綴的數據庫 public void SelectLocalMark() { //生成一個批處理文件 string batpath=Application.StartupPath+"\\back.bat"; string batfile = "back.bat"; if (!File.Exists(batpath)) { FileInfo myinfo = new FileInfo(batpath); FileStream fs = myinfo.Create(); fs.Close(); } //FileInfo myfiles = new FileInfo(batpath); string expstr = ""; string sql = "select * from all_users where username like '%_"+LocalMark.Text.Trim().ToUpper()+"'"; using (OracleCommand cmd = new OracleCommand(sql, orclConnection())) { cmd.Connection.Open(); using (OracleDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { //expstr = "exp " + UserName.Text.Trim() + "/" + Pwd.Text.Trim() + "@" + ServiceName.Text.Trim() + " file=" + dmpPath + "\\back.dmp log=" + dmpPath + "\\back.log owner=" + reader.GetString(reader.GetOrdinal("username")).ToUpper(); ////CMD(expstr); //執行備份語句 ////把備份語句寫入批處理文件 //StreamWriter sm = new StreamWriter(batpath); //sm.WriteLine(expstr); //sm.WriteLine("exit"); //sm.Flush(); //sm.Close(); } } } CMD(batfile); }
上面是我做好的備份實例,測試過,完全通過,其中可能涉及到界面上的一些參數的獲取和我具體要求的代碼,但核心部分就是調用exp.exe備份,備份可直接執行語句,也可以執行批處理文件,看你自己的喜好了~~~
上述實例為個人實例代碼,請勿隨意轉載~