如何进行数据备份
我刚接触asp.net有1周的时间.现在要做一个数据备份的系统。请问用C#如何进行数据备份,请高手指点。急用,谢谢了
------解决方案--------------------网站 添加引用 COM Microsoft SQLServer SQLDMO Library
给你一个类
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;
/// <summary>
/// DBService 的摘要说明
/// </summary>
public class DBService
{
public DBService()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 数据库备份
/// </summary>
/// <param name= "sqlServer "> SQLServer数据库服务器 </param>
/// <param name= "userid "> 用户名 </param>
/// <param name= "password "> 用户密码 </param>
/// <param name= "database "> 要备份的数据库 </param>
/// <param name= "filePath "> 存储备份文件路径 </param>
/// <returns> 是否备份成功 </returns>
public static bool DbBackup(string sqlServer,string userid,string password,string database,string filePath)
{
//声明变量isOk用来返回执行结果
bool isOk;
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(sqlServer,userid, password);
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = database;
oBackup.Files = filePath;
oBackup.BackupSetName = database;
oBackup.BackupSetDescription = "数据库备份 ";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
isOk = true;//执行成功
}
catch
{
isOk = false;//执行失败
//throw;
}
finally
{
oSQLServer.DisConnect();
}
//返回结果
return isOk;
}
/// <summary>
/// 数据库恢复
/// </summary>
/// <param name= "sqlServer "> SQLServer数据库服务器 </param>
/// <param name= "userid "> 用户名 </param>
/// <param name= "password "> 用户密码 </param>
/// <param name= "database "> 要还原的数据库 </param>
/// <param name= "filePath "> 存储备份文件路径 </param>
/// <returns> 是否还原成功 </returns>
public static bool DbRestore(string sqlServer, string userid, string password, string database, string filePath)
{
bool isOk;//变量用来返回执行结果
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(sqlServer, userid, password);
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = database;
oRestore.Files = filePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
isOk = true;
}
catch
{
i