使用c#调用exp.exe导出Oracle数据(经测试有误,留做存档,请勿参考)
2009-04-02 17:14
806 查看
使用C#调用exp.exe导出数据时,exp.exe在完成后无法自动结束,而且很难判断其是否已经导出完成,不过当exp的参数里带有log时,没有完成导出时,log文件大小为0,导出完成后才会有内容,这里通过这个来判断是否已经导出完成,如果导出完成,则停止掉exp.exe,具体代码如下:
主窗体:一些用户交互,并且结束exp.exe
waitForFile函数
窗体2:调用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(""); } }
相关文章推荐
- 在plsql中使用exp imp工具实现oracle数据导出导入(转)
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出
- Oracle处于recover状态的数据文件是无法使用exp导出数据的
- Oracle数据导入导出(imp/exp)命令的使用
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出(转)
- Oracle数据导入导出imp/exp命令使用
- Oracle使用实例:exp和expdp导出数据
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出
- Oracle 11G 用exp无法导出空表解决方法以及数据泵的使用
- Oracle 数据导入导出imp/exp 使用详解
- Oracle Imp and Exp (导入和导出) 数据 工具使用
- [转]使用Oracle 的 imp ,exp 命令实现数据的导入导出
- Oracle Imp and Exp (导入和导出) 数据 工具使用
- 使用exp命令实现Oracle数据备份(数据导出)
- oracle使用exp与imp在本地导入导出数据
- 使用Oracle 的 imp ,exp 命令实现数据的导入导出
- oracle 使用exp命令只导出表结构,不要数据