返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

实现备份,还原sql2000数据库程序代码

时间:2011-05-24 14:11来源:知行网www.zhixing123.cn 编辑:麦田守望者

引入using SQLDMO;
#region 进行数据库的备份
/// <summary>
/// 进行数据库的备份
/// </summary>
/// <param name="servername">备份数据库的服务器名</param>
/// <param name="username">数据库服务器的用户名</param>
/// <param name="password">数据库服务器的密码</param>
/// <param name="basename">要备份的数据库名</param>
/// <param name="backname">备份的数据库名</param>
/// <param name="filename">备份的目标文件</param>
/// <returns></returns>
public static bool BackDataBase(string servername,string username,string password,string basename,string backname,string filename)
{
SQLDMO.Backup backup = new BackupClass();
SQLDMO.SQLServer sqlserver = new SQLServerClass();
try
{
sqlserver.LoginSecure = false;
sqlserver.Connect(servername, username, password);
backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
backup.Database = basename;
backup.Files = filename;
backup.BackupSetName = backname;
backup.BackupSetDescription = "数据库备份";
backup.Initialize = true;
backup.SQLBackup(sqlserver);
return true;
}
catch (Exception ex)
{
ExceptionMessage = ex.Message;
return false;
}
finally
{
sqlserver.DisConnect();
}
}
#endregion

#region 还原数据库
/// <summary>
/// 还原数据库
/// </summary>
/// <param name="servername">数据库服务器名</param>
/// <param name="username">登陆数据库的用户名</param>
/// <param name="password">登陆数据库的密码</param>
/// <param name="restorename">还原后的数据库名</param>
/// <param name="filename">数据库还原文件</param>
/// <param name="restorenumber">还原的数量</param>
/// <returns></returns>
public static bool RestoreDataBase(string servername, string username, string password, string restorename, string filename, int restorenumber)
{
SQLDMO.Restore res = new SQLDMO.RestoreClass();
SQLDMO.SQLServer server = new SQLServerClass();
try
{
server.LoginSecure = false;
server.Connect(servername, username, password);
res.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
res.Database = restorename;
res.Files = filename;
res.FileNumber = restorenumber;
res.ReplaceDatabase = true;
res.SQLRestore(server);
return true;
}
catch(Exception ex)
{
ExceptionMessage = ex.Message;
return false;
}
finally
{
server.DisConnect();
}
}
#endregion

------分隔线----------------------------
标签(Tag):C# C#实例教程 c#基础教程 C#源代码
------分隔线----------------------------
推荐内容
猜你感兴趣