Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Namespace wangshun. systemconfig
{
/// <Summary>
/// Summary of the database.
/// </Summary>
Public class database: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. Button button1;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}
/** // <Summary>
/// Kill all processes in the current database
/// </Summary>
/// <Returns> </returns>
Private bool exepro ()
{
String connstr = system. configuration. configurationsettings. etettings ["connectionstring"];
String [] connstrayy = connstr. Split (";". tochararray (), 4 );
String IP = connstrayy [0]. Split ("=". tochararray (), 2) [1];
String database = connstrayy [1]. Split ("=". tochararray (), 2) [1];
String userid = connstrayy [2]. Split ("=". tochararray (), 2) [1];
String Pwd = connstrayy [3]. Split ("=". tochararray (), 2) [1];
Sqlconnection conn1 = new sqlconnection ("Server =" + IP + "; uid =" + userid + "; Pwd =" + PWD + "; database = Master ");
Sqlcommand cmd = new sqlcommand ("killspid", conn1 );
Cmd. commandtype = commandtype. storedprocedure;
Cmd. Parameters. Add ("@ dbname", database );
Try
{
Conn1.open ();
Cmd. executenonquery ();
Return true;
}
Catch (exception ex)
{
Return false;
}
Finally
{
Conn1.close ();
}
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
/// <Summary>
/// Back up the database
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, system. eventargs E)
{
Sqldmo. Backup obackup = new sqldmo. backupclass ();
Sqldmo. sqlserver osqlserver = new sqldmo. sqlserverclass ();
Try
{
String connstr = system. configuration. configurationsettings. etettings ["connectionstring"];
String [] connstrayy = connstr. Split (";". tochararray (), 4 );
String IP = connstrayy [0]. Split ("=". tochararray (), 2) [1];
String database = connstrayy [1]. Split ("=". tochararray (), 2) [1];
String userid = connstrayy [2]. Split ("=". tochararray (), 2) [1];
String Pwd = connstrayy [3]. Split ("=". tochararray (), 2) [1];
Osqlserver. loginsecure = false;
Osqlserver. Connect (IP, userid, PWD );
Obackup. Action = sqldmo. sqldmo_backup_type.sqldmobackup_database;
Obackup. Database = database;
Obackup. Files = server. mappath ("./dbback") + "/" + database + ". Bak ";
Obackup. backupsetname = database;
Obackup. backupsetdescription = "database backup ";
Obackup. initialize = true;
Obackup. sqlbackup (osqlserver );
Response. Write ("<SCRIPT> alert ('Congratulations, you have backed up the database! '); </SCRIPT> ");
}
Catch
{
Throw;
}
Finally
{
Osqlserver. Disconnect ();
}
}
/// <Summary>
/// Restore the database
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button2_click (Object sender, system. eventargs E)
{
If (exepro ()! = True) // execute the Stored Procedure
{
Response. Write ("<SCRIPT> alert ('Sorry, the operation failed. Please try again! '); </SCRIPT> ");
Return;
}
Else
{
Sqldmo. Restore orestore = new sqldmo. restoreclass ();
Sqldmo. sqlserver osqlserver = new sqldmo. sqlserverclass ();
Try
{
String connstr = system. configuration. configurationsettings. etettings ["connectionstring"];
String [] connstrayy = connstr. Split (";". tochararray (), 4 );
String IP = connstrayy [0]. Split ("=". tochararray (), 2) [1];
String database = connstrayy [1]. Split ("=". tochararray (), 2) [1];
String userid = connstrayy [2]. Split ("=". tochararray (), 2) [1];
String Pwd = connstrayy [3]. Split ("=". tochararray (), 2) [1];
Osqlserver. loginsecure = false;
Osqlserver. Connect (IP, userid, PWD );
Orestore. Action = sqldmo. sqldmo_restore_type.sqldmorestore_database;
Orestore. Database = database;
Orestore. Files = server. mappath ("./dbback") + "/" + database + ". Bak ";
Orestore. filenumber = 1;
Orestore. replacedatabase = true;
Orestore. sqlrestore (osqlserver );
Response. Write ("<SCRIPT> alert ('Congratulations, your database has been restored successfully! '); </SCRIPT> ");
}
Catch
{
Throw;
}
Finally
{
Osqlserver. Disconnect ();
}
}
}
}
}
Front-end