Using SQLDMO to back up and restore Microsoft SQL Server databases in C #

Source: Internet
Author: User
Tags microsoft sql server

SQLDMO (SQL distributed Management objects,sql Distributed Management Objects) encapsulates objects in a Microsoft SQL Server database. SQLDMO is the application interface used by Enterprise Manager in Microsoft SQL Server, so it can perform many functions, including, of course, backup and recovery of the database.

SQLDMO is provided by Microsoft SQL Server SQLDMO.dll, and since SQLDMO.dll is a COM object, you must add a reference to it in the. NET project before you use it

The following is a class written in the C # language for backup and recovery of Microsoft SQL Server databases:

Using System;
Namespace Dbservice
{
<summary>
Dboper class, which primarily applies sqldmo to backup and restore Microsoft SQL Server databases
</summary>
public sealed class Dboper
{
<summary>
Constructors for Dboper classes
</summary>
Private Dboper ()
{
}
<summary>
Database backup
</summary>
public static void DbBackup ()
{
SQLDMO. Backup obackup = new SQLDMO. Backupclass ();
SQLDMO. SQL Server 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 ();
}
}
<summary>
Database recovery
</summary>
public static void Dbrestore ()
{
SQLDMO. Restore Orestore = new SQLDMO. Restoreclass ();
SQLDMO. SQL Server 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
{
Osqlserver.disconnect ();
}
}
}
}

Although this code is very short, but very practical, I hope to be able to help you:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.