asp.net实现数据库备份和还原

asp.net实现数据库备份和还原sql2005的最好给源码。谢谢啦!急用啊!... asp.net实现数据库备份和还原 sql2005的
最好给源码。谢谢啦!
急用啊!
展开
 我来答
leon382
2010-08-21 · TA获得超过1064个赞
知道小有建树答主
回答量:1744
采纳率:0%
帮助的人:984万
展开全部
//使用前须先引用SQLDMO.dll ADOX.dll JRO.dll 等组件,无须独立安装
//如:C:\Program Files\Common Files\System\ado\msadox.dll
//如:C:\Program Files\Common Files\System\ado\msjro.dll
//如:C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SQLDMO.DLL

using System;
using System.IO;

/// <summary>
/// 数据库恢复和备份
/// </summary>
public class DataBak
{
public DataBak()
{
//
// TO

//DO: 在此处添加构造函数逻辑
//
}

#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 bool BakSql(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.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = DBName;
oBackup.Files = BackPath;
oBackup.BackupSetName = DBName;
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
return true;
}
catch (Exception e)
{
return false;
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 bool RecoverSql(string ServerIP, string LoginName, string LoginPass, string DBName, string BackPath)
{

SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
//杀死所有SQL进程
SQLDMO.QueryResults qr = oSQLServer.EnumProcesses(-1);
int iColPIDNum = -1;
int iColDbName = -1;
for (int i = 1; i <= qr.Columns; i++)
{
string strName = qr.get_ColumnName(i);
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i;
}
else if (strName.ToUpper().Trim() == "DBNAME")
{
iColDbName = i;
}
if (iColPIDNum != -1 && iColDbName != -1)
break;
}

for (int i = 1; i <= qr.Rows; i++)
{
int lPID = qr.GetColumnLong(i, iColPIDNum);
string strDBName = qr.GetColumnString(i, iColDbName);
if (strDBName.ToUpper() == DBName.ToUpper())
oSQLServer.KillProcess(lPID);
}

//执行还原工作
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);
return true;
}
catch (Exception e)
{
return false;
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion

#region 备份SQL数据库
/// <summary>
/// 备份SQL数据库
/// </summary>
/// <param name="databasename">要备份的数据源名称</param>
/// <param name="backuptodatabase">备份到的数据库文件名称及路径</param>
/// <returns></returns>
public static bool BakSql(string databaseName, string bakPath)
{
try
{
//声明临时数据库名称
string bakName = databaseName;

bakName += DateTime.Now.Year.ToString();
bakName += DateTime.Now.Month.ToString();
bakName += DateTime.Now.Day.ToString();
bakName += DateTime.Now.Hour.ToString();
bakName += DateTime.Now.Minute.ToString();
bakName += DateTime.Now.Second.ToString();
bakName += ".bak";

if (bakPath.LastIndexOf('\\') != bakPath.Length)
bakPath += '\\';

string sqlbak = "BACKUP DATABASE " + databaseName + " TO DISK = '" + bakPath + bakName + "' WITH INIT";

if (Common.ExeSql(sqlbak) != 0)
return true;
else
return false;
}
catch
{
return false;
}
}
#endregion

#region 还原SQL数据库

/// <summary>
/// 还原指定的数据库文件
/// </summary>
/// <param name="databasename">要还原的数据库</param>
/// <param name="databasefile">数据库备份文件及路径</param>
/// <returns></returns>
public static bool RecoverSql(string databasename, string databasefile)
{
try
{
//还原指定的数据库文件
string sql = "RESTORE DATABASE " + databasename + " FROM DISK = '" + databasefile + "' ";

if (Common.ExeSql(sql) != 0)
return true;
else
return false;
}
catch
{
return false;
}
}
#endregion

#region 根据指定的文件名称创建Access数据库
/// <summary>
/// 根据指定的文件名称创建数据
/// </summary>
/// <param name="mdbPath">绝对路径+文件名称</param>
public static bool CreateAccess(string mdbPath)
{
if (File.Exists(mdbPath))//检查数据库是否已存在
{
return false;
}
else
{
try
{
mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
//创建一个CatalogClass对象实例
ADOX.CatalogClass cat = new ADOX.CatalogClass();
//使用CatalogClass对象的Create方法创建ACCESS数据库
cat.Create(mdbPath);
return true;
}
catch
{
return false;
}
}
}
#endregion

#region 压缩Access数据库
/// <summary>
/// 压缩Access数据库
/// </summary>
/// <param name="mdbPath">数据库绝对路径</param>
public static bool CompactAccess(string mdbPath)
{
try
{
//声明临时数据库名称
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";

string srcconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
string destPath = mdbPath.Substring(0, mdbPath.LastIndexOf("\\") + 1) + temp;
string destconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + destPath;

//创建一个JetEngineClass对象的实例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass对象的CompactDatabase方法压缩修复数据库
jt.CompactDatabase(srcconn, destconn);
//拷贝临时数据库到目标数据库(覆盖)
File.Copy(destPath, mdbPath, true);
//最后删除临时数据库
File.Delete(destPath);
return true;
}
catch
{
return false;
}
}
#endregion

#region 备份Access数据库
/// <summary>
/// 备份Access数据库
/// </summary>
/// <param name="srcPath">要备份的数据库绝对路径</param>
/// <param name="aimPath">备份到的数据库绝对路径</param>
/// <returns></returns>
public static bool BakAccess(string srcPath, string aimPath)
{
try
{
//声明临时数据库名称
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";

string srcconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srcPath;
string destconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + aimPath.Substring(0, aimPath.LastIndexOf(".") + 1) + temp;

//创建一个JetEngineClass对象的实例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass对象的CompactDatabase方法压缩修复数据库
jt.CompactDatabase(srcconn, destconn);
return true;
}
catch
{
return false;
}
}

#endregion

#region 还原Access数据库
/// <summary>
/// 还原Access数据库
/// </summary>
/// <param name="bakPath">备份的数据库绝对路径</param>
/// <param name="mdbPath">要还原的数据库绝对路径</param>
public static bool RecoverAccess(string bakPath, string mdbPath)
{
try
{
File.Copy(bakPath, mdbPath, true);
return true;
}
catch
{
return false;
}
}
#endregion
}
百度网友e7b6a5ad0
2010-08-25 · TA获得超过175个赞
知道小有建树答主
回答量:222
采纳率:100%
帮助的人:209万
展开全部
SQLDMO类
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-08-21
展开全部
Mark!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
黑色包小公
2010-09-03 · TA获得超过4471个赞
知道小有建树答主
回答量:532
采纳率:0%
帮助的人:1229万
展开全部
备份:133
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式