[Post] Stored Procedure for remote SQL Server database backup and recovery under ASP. NET

Source: Internet
Author: User

The help document address of SQL Server. Just enter it in IE.
MK: @ msitstore: C: \ Program % 20files \ microsoft % 20sql % 20server \ 80 \ tools \ books \ tsqlref. chm:/ts_ba-bz_35ww.htm

Backup database to disk path backup
Restore database from disk path

Using system;
Using system. collections;
Using system. Security. cryptography;

Using system. Data;
Using system. Data. sqlclient;
Using system. Web;

Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Using system. diagnostics;
Using system. text;
Using system. componentmodel;
Using system. configuration;
Using system. Data. oledb;

Namespace dbservice
{
/// <Summary>
/// Dboper class, mainly used to back up and restore Microsoft SQL Server databases
/// </Summary>
Public sealed class dboper
{
/// <Summary>
/// Dboper Constructor
/// </Summary>
// Private dboper ()
//{
//}

/// <Summary>
/// Database Backup
/// </Summary>
///
Public static string backfilename = system. Web. httpcontext. Current. Request. physicalapplicationpath + "backup \ mydbback. Bak ";
Public static void dbbackup (string dbnanme, string userid, string PWD)
{
Try
{
Sqldmo. Backup obackup = new sqldmo. backupclass ();
Sqldmo. sqlserver osqlserver = new sqldmo. sqlserverclass ();
Osqlserver. loginsecure = false;
Osqlserver. Connect ("localhost", userid, PWD );
Obackup. Action = sqldmo. sqldmo_backup_type.sqldmobackup_database;
Obackup. Database = dbnanme;
Obackup. Files = backfilename;
Obackup. backupsetname = dbnanme;
Obackup. backupsetdescription = "database backup ";
Obackup. initialize = true;
Obackup. sqlbackup (osqlserver );
}
Catch
{
Throw;
}
}

/// <Summary>
/// Restore database functions
/// </Summary>
/// <Param name = "strdbname"> database name </param>
/// <Param name = "strfilename"> full path name of the database backup file </param>
/// <Returns> </returns>
Public bool restoredb (string strdbname, string strfilename, string userid, string PWD)
{
// Pbar = pgbmain;
Sqldmo. svr SVR = new sqldmo. sqlserverclass ();
Try
{
// Server name, database username, database username and password
SVR. Connect ("localhost", userid, PWD );

Sqldmo. queryresults QR = SVR. enumprocesses (-1 );
Int icolpidnum =-1;
Int icoldbname =-1;
For (INT I = 1; I <= QR. columns; I ++)
{
String strname = QR. get_columnname (I );
If (strname. toupper (). Trim () = "spid ")
{
Icolpidnum = I;
}
Else if (strname. toupper (). Trim () = "dbname ")
{
Icoldbname = I;
}
If (icolpidnum! =-1 & icoldbname! =-1)
Break;
}
// Kill the process that uses the strdbname Database
For (INT I = 1; I <= QR. Rows; I ++)
{
Int LPID = QR. getcolumnlong (I, icolpidnum );
String strdbname = QR. getcolumnstring (I, icoldbname );
If (strdbname. toupper () = strdbname. toupper ())
{
SVR. killprocess (LPID );
}
}

Sqldmo. Restore res = new sqldmo. restoreclass ();
Res. Action = 0;
Res. Files = strfilename;

Res. Database = strdbname;
Res. replacedatabase = true;
Res. sqlrestore (SVR );
Return true;
}
Catch
{
Return false;
}
Finally
{
SVR. Disconnect ();
}
}

}
}

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.