欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 数据库备份还原(sqlserver+access)

程序员文章站 2022-06-08 17:53:09
/********************************************************************************** *...
/**********************************************************************************
*
* 功能说明:备份和恢复sql server数据库
* 作者: 刘功勋;
* 版本:v0.1(c#2.0);时间:2007-1-1
* 当使用sql server时,请引用 com组件中的,sqldmo.dll组件
* 当使用access中,请浏览添加引用以下两个dll
* 引用c:\program files\common files\system\ado\msadox.dll,该dll包含adox命名空间
* 引用c:\program files\common files\system\ado\msjro.dll,该dll包含jro命名空间
* *******************************************************************************/
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.io;
using adox;//该命名空间包含创建access的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dll
using jro;//该命名空间包含压缩access的类(方法)
namespace ec
{
/// <summary>
/// 数据库恢复和备份
/// </summary>
public class sqlbackobject
{
public sqlbackobject()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region sql数据库备份
/// <summary>
/// sql数据库备份
/// </summary>
/// <param name="serverip">sql服务器ip或(localhost)</param>
/// <param name="loginname">数据库登录名</param>
/// <param name="loginpass">数据库登录密码</param>
/// <param name="dbname">数据库名</param>
/// <param name="backpath">备份到的路径</param>
public static void sqlback(string serverip,string loginname,string loginpass,string dbname,string backpath)
{
sqldmo.backup obackup = new sqldmo.backupclass();
sqldmo.sqlserver osqlserver = new sqldmo.sqlserverclass();
try
{
osqlserver.loginsecure = false;
osqlserver.connect(serverip, loginname, loginpass);
obackup.database = dbname;
obackup.files = backpath;
obackup.backupsetname = dbname;
obackup.backupsetdescription = "数据库备份";
obackup.initialize = true;
obackup.sqlbackup(osqlserver);
}
catch (exception e)
{
throw new exception(e.tostring());
}
finally
{
osqlserver.disconnect();
}
}
#endregion
#region sql恢复数据库
/// <summary>
/// sql恢复数据库
/// </summary>
/// <param name="serverip">sql服务器ip或(localhost)</param>
/// <param name="loginname">数据库登录名</param>
/// <param name="loginpass">数据库登录密码</param>
/// <param name="dbname">要还原的数据库名</param>
/// <param name="backpath">数据库备份的路径</param>
public static void sqldbrestore(string serverip,string loginname,string loginpass,string dbname,string backpath)
{
sqldmo.restore orestore = new sqldmo.restoreclass();
sqldmo.sqlserver osqlserver = new sqldmo.sqlserverclass();
try
{
osqlserver.loginsecure = false;
osqlserver.connect(serverip, loginname, loginpass);
orestore.action = sqldmo.sqldmo_restore_type.sqldmorestore_database;
orestore.database = dbname;
orestore.files = backpath;
orestore.filenumber = 1;
orestore.replacedatabase = true;
orestore.sqlrestore(osqlserver);
}
catch (exception e)
{
throw new exception(e.tostring());
}
finally
{
osqlserver.disconnect();
}
}
#endregion
#region 根据指定的文件名称创建access数据库
/// <summary>
/// 根据指定的文件名称创建数据
/// </summary>
/// <param name="dbpath">绝对路径+文件名称</param>
public static void createaccess(string dbpath)
{
if (file.exists(dbpath))//检查数据库是否已存在
{
throw new exception("目标数据库已存在,无法创建");
}
dbpath = "provider=microsoft.jet.oledb.4.0;data source="+dbpath;
//创建一个catalogclass对象实例
adox.catalogclass cat = new adox.catalogclass();
//使用catalogclass对象的create方法创建access数据库
cat.create(dbpath);
}
#endregion
#region 压缩access数据库
/// <summary>
/// 压缩access数据库
/// </summary>
/// <param name="dbpath">数据库绝对路径</param>
public static void compactaccess(string dbpath)
{
if (!file.exists(dbpath))
{
throw new exception("目标数据库不存在,无法压缩");
}
//声明临时数据库名称
string temp = datetime.now.year.tostring();
temp += datetime.now.month.tostring();
temp += datetime.now.day.tostring();
temp += datetime.now.hour.tostring();
temp += datetime.now.minute.tostring();
temp += datetime.now.second.tostring() + ".bak";
temp = dbpath.substring(0, dbpath.lastindexof("\\") + 1) + temp;
//定义临时数据库的连接字符串
string temp2 = "provider=microsoft.jet.oledb.4.0;data source="+temp;
//定义目标数据库的连接字符串
string dbpath2 = "provider=microsoft.jet.oledb.4.0;data source="+dbpath;
//创建一个jetengineclass对象的实例
jro.jetengineclass jt = new jro.jetengineclass();
//使用jetengineclass对象的compactdatabase方法压缩修复数据库
jt.compactdatabase(dbpath2, temp2);
//拷贝临时数据库到目标数据库(覆盖)
file.copy(temp, dbpath, true);
//最后删除临时数据库
file.delete(temp);
}
#endregion
#region 备份access数据库
/// <summary>
/// 备份access数据库
/// </summary>
/// <param name="srcpath">要备份的数据库绝对路径</param>
/// <param name="aimpath">备份到的数据库绝对路径</param>
/// <returns></returns>
public static void backup(string srcpath,string aimpath)
{
if (!file.exists(srcpath))
{
throw new exception("源数据库不存在,无法备份");
}
try
{
file.copy(srcpath,aimpath,true);
}
catch(ioexception ixp)
{
throw new exception(ixp.tostring());
}
}
#endregion
#region 还原access数据库
/// <summary>
/// 还原access数据库
/// </summary>
/// <param name="bakpath">备份的数据库绝对路径</param>
/// <param name="dbpath">要还原的数据库绝对路径</param>
public static void recoveraccess(string bakpath,string dbpath)
{
if (!file.exists(bakpath))
{
throw new exception("备份数据库不存在,无法还原");
}
try
{
file.copy(bakpath, dbpath, true);
}
catch (ioexception ixp)
{
throw new exception(ixp.tostring());
}
}
#endregion
}
}