C#oracle备份和还原
最近公司的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(, tables.Length - ); //记录备份的所有方案名到一个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(, tables.Length - );
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备份,备份可直接执行语句,也可以执行批处理文件,看你自己的喜好了~~~
上述实例为个人实例代码,请勿随意转载~
C#oracle备份和还原的更多相关文章
- Oracle 备份与还原
oracle 备份与还原 一.备份数据库(exp) 1.完全备份 exp demo/demo@orcl buffer=1024 file=d:\back.dmp full=y demo:用户名.密码 ...
- oracle 备份与还原 及相关操作
drop user 用户名 cascade; ........删除用户 create user 用户名 identified by 密码 default tablespace 数据文件名 tempor ...
- oracle备份与还原(导入导出)
Oracle数据导入导出imp/exp 功能:Oracle数据导入导出imp/exp相当于oracle数据还原与备份.说明:大多情况都可以用Oracle数据导入导出完成数据的备份和还原(不会造成数据的 ...
- oracle备份与还原数据
一.表数据备份与还原 creat table 备份表 select * from 原表 where insert into 原表 select * from 备份表 二.利用备份 ...
- oracle 备份和还原还有创建用户、表空间、授权
--找到存放dbf文件的路径--E:\oracle\product\10.2.0\oradata\orcl--可以通过此语句进行查询select * from v$datafile; --创建表空间c ...
- oracle备份、还原
----第一步:设置空表导出 ----由于Oracle 11G在用EXPORT导出时,空表不能导出.解决方法如下:(11G中有个新特性,当表无数据时,不分配segment,以节省空间) select ...
- Oracle备份、还原数据库
备份数据库 创建备份目录(用sys账号),若已创建备份目录,此步可忽略 create directory db_bak as 'D:\ ECIMS_DB' --查看创建的目录 select * fro ...
- oracle备份和还原
用exp命令即可完成,但要看具体的备份方式. 1. 导出一个完整数据库 exp system/manager file=bible_db log=dible_db full=y 2. 导出数据库定义而 ...
- oracle数据泵备份与还原
完整的常用的一套oracle备份以及还原方案 --在新库中新建数据目录,我没有特别说明在哪执行的语句都可在plsql中执行 CREATE OR REPLACE DIRECTORY dump_dir A ...
随机推荐
- hdu-4990 Reading comprehension(快速幂+乘法逆元)
题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
- bzoj2330糖果——差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束,再建立一个源点0,向所有点连边权为1的边,表示每个人都会分到糖果: 答案较大 ...
- CentOS 6 命令行下安装 VirtualBox 虚拟机步骤
CentOS 6 命令行下安装 VirtualBox 虚拟机步骤 1. 准备工作 安装内核更新 yum install kernel-develyum update kernel*如果内核有更新,则需 ...
- 4.js屏蔽浏览器鼠标右键菜单
document.oncontextmenu = function(){return false;} 参考链接:js 屏蔽浏览器事件汇总
- Ubuntu 安装indicator-sysmonitor
之前就像安装一个软件用来查看Ubuntu的CPU, 内存, 网速情况, 终于让我碰到了--indicator-sysmonitor 仅需三条命令, 你值得拥有: sudo add-apt-reposi ...
- kvm详细介绍
KVM详解,太详细太深入了,经典 2016-07-18 19:56:38 分类: 虚拟化 原文地址:KVM详解,太详细太深入了,经典 作者:zzjlzx KVM 介绍(1):简介及安装 http:// ...
- 1.SJ-SLAM-14
1.引言 SLAM:Simultaneous Localization and Mapping 同时定位与地图构建 搭载特定传感器的主体,在没有环境先验信息的情况下,于运动过程中建立环境的模型,同时估 ...
- ASP.NET Core会议管理平台实战_2、基本概念的理解
id Token携带用户的信息 AccessToken:是否有权限访问资源 看数据库的表,Client相关的表,api的相关的表 Resources把用户的简介抽象出来到IdentityClaims表 ...
- c#类—成员函数和封装及构造函数、析构函数、静态成员
C# 类(Class) 当您定义一个类时,您定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...