使用c#调用exp.exe导出Oracle数据(经测试有误,留做存档,请勿参考)

 使用C#调用exp.exe导出数据时,exp.exe在完成后无法自动结束,而且很难判断其是否已经导出完成,不过当exp的参数里带有log时,没有完成导出时,log文件大小为0,导出完成后才会有内容,这里通过这个来判断是否已经导出完成,如果导出完成,则停止掉exp.exe,具体代码如下:

 

主窗体:一些用户交互,并且结束exp.exe

try { //打开导出窗体 sanxiaExpDlg expDlg1 = new sanxiaExpDlg(); expDlg1.userName = userNameBox.Text; expDlg1.passWord = passwordBox.Text; expDlg1.targetPath = targetPath; expDlg1.ShowDialog(); expDlg1.Dispose(); //kill掉exp.exe Process[] MyProcess = Process.GetProcessesByName("EXP.EXE"); if (MyProcess.Length != 0) { MyProcess[0].Kill(); } //判断文件是否仍在被占用,如果被占用,则等待 waitForFile(targetPath + "//explog.log"); FileStream fs = File.Open(targetPath + "//explog.log",FileMode.Open,FileAccess.Read,FileShare.None); StreamReader sr = new StreamReader(fs,Encoding.GetEncoding("gb2312")); string logString = sr.ReadToEnd(); sr.Close(); File.Delete(targetPath+"//explog.log"); this.richTextBox1.Text = logString; if (logString.IndexOf("错误") != -1)//导出错误 { MessageBox.Show("导出失败"); return; } } catch (Exception ex) { MessageBox.Show(ex.Message); return; }

 

waitForFile函数

private int waitForFile(string fileName) { if (!File.Exists(fileName)) { return 0; // 0 文件不存在 } while (true) { try { FileStream fs = File.Open(fileName,FileMode.Open,FileAccess.Read,FileShare.None); fs.Close(); return 1; } catch (Exception ex) { } } }

 

窗体2:调用exp.exe来进行导出,并判断导出是否完成

public partial class testExpDlg :Form { public testExpDlg() { InitializeComponent(); } public string userName = ""; public string passWord = ""; public string targetPath = ""; //由于这里使用timer来关闭窗体,由于线程间的访问权限,需要用到委托 private delegate void CloseFormDelegate(string value); private void CloseForm(string value) { if (this.InvokeRequired) { CloseFormDelegate d = new CloseFormDelegate(CloseForm); this.Invoke(d,new object[] { value}); } else { this.Close(); } } private void testExpDlg_Load(object sender,EventArgs e) { //声明一个timer System.Timers.Timer timer2 = new System.Timers.Timer(500); timer2.AutoReset = false; timer2.Enabled = true; timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed); timer2.Start(); } void timer2_Elapsed(object sender,System.Timers.ElapsedEventArgs e) { try { // 实例一个Process类,启动一个独立进程 Process p = new Process(); // 设定程序名 p.StartInfo.FileName = "cmd.exe"; //设定参数 //p.StartInfo.Arguments = "/c exp " + userName + "/" + passWord + "@sanxia owner=sanxia file=" + targetPath + "//exp.dmp log=" + targetPath + "//explog.log"; p.StartInfo.Arguments = "/c exp " + userName + "/" + passWord + "@test tables=sde.sde_blk_45 file=" + targetPath + "//exp.dmp log=" + targetPath + "//explog.log"; // 关闭Shell的使用 p.StartInfo.UseShellExecute = false; // 重定向标准输入 p.StartInfo.RedirectStandardInput = true; // 重定向标准输出 p.StartInfo.RedirectStandardOutput = true; //重定向错误输出 p.StartInfo.RedirectStandardError = true; // 设置不显示窗口 p.StartInfo.CreateNoWindow = true; // 启动进程 p.Start(); System.Threading.Thread.Sleep(500); //查看log文件的大小,若为0则代表导出未完成,若不为0,则代表导出已经完成,这时将进程kill掉,并关闭本窗体 while (true) { FileInfo fi = new FileInfo(targetPath + "//explog.log"); if (fi.Length != 0) { p.Kill(); break; } } } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } CloseForm(""); } }

你可能感兴趣的:(oracle,timer,exception,String,C#,测试)