首页 > 技术文章 > C#oracle还原imp实例

sguozeng 2016-03-15 17:38 原文

C#来做oracle还原,调用oracle自带函数imp.exe时,需要注意的是:
1、imp.exe 中fromuser 和touser两个关键字; fromuser与exp.exe中的owner对应,为表的所有者,都可以是多个参数,如:fromuser=(A,B,C)
2、imp.exe 进行恢复数据库时,要确定一下恢复用户(方案)的表空间,一般默认为users空间
3、在获取到表空间权限后,要对用户进行恢复权限设置,主要是对表空间的限制,一般处理:grant unlimited tablespace to user(自己的用户)
4、程序中设置了oracle服务端的exp和imp的路径
注意:在C#做备份和还原的时候,都是调用的oracle服务器的自带函数
   在用进程来调用备份和还原时,备份完成和结束后,要进行进程释放。


//
开始恢复数据库 private void button1_Click(object sender, EventArgs e) { string tables = ""; //创建要还原的方案 //从备份的方案中读取 FileStream fs=new FileStream (textBox6.Text,FileMode.Open); StreamReader smread=new StreamReader(fs); smread.BaseStream.Seek(0,SeekOrigin.Begin); string strLine = ""; string backusers = ""; while ((strLine=smread.ReadLine()) != null) { backusers += strLine + ","; } if (backusers.Length > 0) { backusers = backusers.Substring(0, backusers.Length - 1); tables = backusers; } String[]userArr=backusers.Split(','); string sqluser=""; String[] marks; string newtables = ""; using (OracleConnection oracon = orclConnection()) { try { oracon.Open(); } catch (Exception ex) { MessageBox.Show("链接数据库错误,错误原因:"+ex.Message.ToString()); } using (OracleTransaction tran = oracon.BeginTransaction()) { using (OracleCommand com = new OracleCommand()) { com.Transaction = tran; string newuser = ""; com.Connection = oracon; //com.Connection.Open(); foreach (string s in userArr) { newuser = s; //判断是否按照默认的标识进行还原 if (!checkBox1.Checked)//不默认 { newuser = ""; //tables = ""; //修改还原的方案名称后缀(标识) marks = s.Split('_'); //修改标识 marks[marks.Count() - 1] = textBox2.Text.ToUpper(); foreach (string mark in marks) { newuser += mark + "_"; } newuser = newuser.Substring(0, newuser.Length - 1); newtables += newuser + ","; } com.CommandText = "Select count(*) from all_users where username='" + newuser + "'"; int i = Convert.ToInt32(com.ExecuteScalar()); //如果存在,删除后还原 if (i > 0) { sqluser = "drop user "+newuser+" cascade"; com.CommandText = sqluser; try { com.ExecuteNonQuery(); com.CommandText = "create user " + newuser + " identified by " + newuser + " default tablespace users"; com.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); tran.Rollback(); } } if (i == 0) { sqluser = "create user " + newuser + " identified by " + newuser+" default tablespace users"; com.CommandText = sqluser; try { com.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); tran.Rollback(); } } sqluser = "grant connect,resource,dba to " + newuser; com.CommandText = sqluser; com.ExecuteNonQuery(); sqluser = "alter user system quota unlimited on users" ; com.CommandText = sqluser; com.ExecuteNonQuery(); //指定还原的方案为还原默认的表空间 sqluser = "grant unlimited tablespace to "+newuser ; com.CommandText = sqluser; com.ExecuteNonQuery(); } if (!checkBox1.Checked) { tables = newtables.Substring(0, newtables.Length - 1); } tran.Commit(); } } } //} string filename = textBox5.Text;//恢复文件路径 //导入程序路径 Process p = new Process(); //p.StartInfo.FileName= "D:\\app\\oracle\\product\\11.2.0\\dbhome_1\\BIN\\imp.exe"; p.StartInfo.FileName = System.Configuration.ConfigurationSettings.AppSettings["pathrestroe"].ToString(); p.StartInfo.UseShellExecute = true; p.StartInfo.CreateNoWindow = false; p.StartInfo.Arguments = "system/system@orcl file=" + filename + " fromuser=("+backusers+") touser=(" +tables+ ") ignore=y" ; //p.StartInfo.Arguments = "system/system@orcl file=" + filename + " fromuser=HYGISTK_CONTROLLINE_ZL2 touser=HYGISTK_CONTROLLINE_ZL ignore=y"; p.Start(); p.WaitForExit(); p.Dispose(); }

 

推荐阅读