Use SQLDMO backup and restore Microsoft SQL Server database in C #
SQLDMO (SQL Distributed Management Objects, SQL Distributed Management Object) encapsulates objects in the Microsoft SQL Server database. SQLDMO is the application interface used by the Enterprise Manager in Microsoft SQL Server, so it can perform a lot of features, which of course also include backups and recovery of the database.
SQLDMO is provided by Microsoft SQL Server SqldMo.dll, since SqldMo.dll is a COM object, so everyone must add a reference to it in .NET project, as shown below:
Below is a class with C # language written in Microsoft SQL Server Database Backup and Recovery:
Using system;
Namespace DBService {/// /// DBOPER class, mainly applying SQLDMO to back up and restore the Microsoft SQL Server database /// summary> public sealed class dboper {/// /// DBOPER Class constructor /// summary> private dboper () {}
/// /// database backup /// summary> public static void dbackup () {sqldmo.backup Obackup = new sqldmo.backupclass (); sqldmo.sqlserver osqlserver = new sqldmo.sqlserverClass (); try {oSQLServer.LoginSecure = false; oSQLServer.Connect ( "localhost", "sa", "1234"); oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; oBackup.Database = "Northwind"; oBackup.Files = @ "d: /Northwind.bak "; oBackup.BackupSetName =" Northwind "; oBackup.BackupSetDescription =" database backup "; oBackup.Initialize = true; oBackup.SQLBackup (oSQLServer);} catch {throw;} finally {oSQLServer.DisConnect (); }
/// /// database recovery /// summary> public static void dbrestore () {sqldmo.restore ore = new sqldmo.restoreClass (); sqldmo.sqlserver osqlserver = new sqldmo.sqlserverClass (); try {oSQLServer.LoginSecure = false; oSQLServer.Connect ( "localhost", "sa", "1234"); oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; oRestore.Database = "Northwind"; oRestore.Files = @ "d: /NORTHWIND.BAK "; orestore.filenumber = 1; orestore.replacedatabase = true; orestore.sqlrestore (osqlserver);} catch {throw;} finally {osqlser.disconnect ();}}}} This code is very short, But it is very practical, I hope to help everyone :)