SQL database backup and restoration on the page

Source: Internet
Author: User

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

 

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.