请问:如何在C#中对数据库进行备份与还原操作?是否要用第三方工具?
小弟最近在做一个毕业设计的项目:超市销存系统,用C#学的。不会在程序中对数据库进行备份与还原。想请各位大哥大姐指点。小弟不胜感激~~~
------解决方案--------------------用封装好的 Interop.SQLDMO.dll 组件
/// <summary>
/// 数据库备份
/// </summary>
/// <returns> 备份是否成功 </returns>
public bool DbBackup ()
{
SQLDMO.Backup DbBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServerClass();
try
{
if (userID != " " && password != " ")
{
sqlServer.LoginSecure = false;
}
else
{
sqlServer.LoginSecure = true;
}
//连接要备份的数据库
sqlServer.Connect(serverName, userID, password);
DbBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
//设置要备份数据库的数据库名称,备份文件,以及相关信息
DbBackup.Database = databaseName;
DbBackup.Files = backUpFile;
DbBackup.BackupSetName = databaseName;
DbBackup.BackupSetDescription = "数据库备份 ";
DbBackup.Initialize = true;
//备份数据库
DbBackup.SQLBackup(sqlServer);
return true;
}
catch
{
throw new BackUpException();
}
finally
{
sqlServer.DisConnect();
}