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 ();
}
}
}
}