31.1:(若用程序调用cmd,则在备份和还原 末尾语句加上 2>&1 ,直接运行则不需要加)
--备份:
1) exp SA/"""abc@1234"""@192.168.31.1:1521/DB1 buffer=10240000 file=C:akdata_back.dmp log=C:akdata_bak_explog.txt owner=(sa,user1) 2>&1
--还原:还原前先执行删除,后执行还原
1) sqlplus sa/"""abc@1234"""@192.168.31.1:1521/DB1 @C:akdeleteDBObjects.txt
2) imp SA/"""abc@1234"""@192.168.31.1:1521/DB1 file=C:akdata_back.dmp full=y ignore=y 2>&1
deleteDBObjects.txt内容:
declare vsql varchar2(2000);
begin
--删除表
BEGIN
for i in (select 'drop table '||owner||'.'||table_name||' cascade constraints' v_name
from dba_tables where owner='SA' or owner='user1')
loop
vsql:=i.v_name;
--dbms_output.put_line(vsql);
execute immediate vsql;
end loop;
end;
--删除过程和函数
BEGIN
for i in (select 'drop '|| object_type||' '||owner||'.' || object_name||'' v_name
from dba_objects where (object_type='PROCEDURE' or object_type='FUNCTION' or object_type='PACKAGE') and (owner='SA' or owner='user1')
)
loop
vsql:=i.v_name;
--dbms_output.put_line(vsql);
execute immediate vsql;
end loop;
end;
end;
/
exit;
相关代码如下:
public async void ExecuteCmd()
{
try
{
Process[] MyProcess = Process.GetProcessesByName("exp");//获取已经存在的exp.exe和imp.exe进程
MyProcess.ToList().AddRange(Process.GetProcessesByName("imp"));
if (MyProcess.Length != 0)
{
for (int i = 0; i < MyProcess.Length; i++)
{
MyProcess[i].Kill();//关闭已存在的进程
MyProcess[i].Dispose();
}
}
string cmd = txtInput.Text;// strInput;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";// @"D:MyOracleOracle11gproduct11.2.0dbhome_1BINexp.exe";
//启用操作系统外壳程序执行
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler(P_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(P_OutputDataReceived);
p.Start(); //设置自动刷新缓冲并更新
//p.StandardInput.AutoFlush = true; //写入命令
p.StandardInput.AutoFlush = true; //写入命令
p.StandardInput.WriteLine(cmd); //p.StandardInput.WriteLine(strInput);
p.StandardInput.WriteLine("exit"); //等待结束
p.BeginOutputReadLine();
p.WaitForExit();
}
catch (Exception ex)
{
}
}
delegate void AsynUpdateUI(string msg);
private void UpdataUIStatus(string msg)
{
if (InvokeRequired)
{
this.Invoke(new AsynUpdateUI(delegate (string argMsg)
{
txtShow.AppendText(argMsg + Environment.NewLine);
}), msg);
}
else
{
txtShow.AppendText(msg + Environment.NewLine);
}
}
private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string x = e.Data.ToString();
UpdataUIStatus(x);
}
}
private void btnBAKUP_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(this.ExecuteCmd));
t.Start();
}