. Net database backup restoration method

Source: Internet
Author: User

Regular backup of web site data is indispensable. We often encounter the website backup function on the web site. Next we will introduce the asp tutorial.. net.

Using System. Data. SqlClient;

Using System. IO;
String SqlStr1 = "Server = (local); DataBase = master; Uid = sa; Pwd = ";
String SqlStr2 = "Exec sp_helpdb ";
String SqlStr1 = "Server = (local); database = '" + this. DropDownList1.SelectedValue + "'; Uid = sa; Pwd = ";
String SqlStr2 = "backup database" + this. DropDownList1.SelectedValue + "to disk = '" + this. TextBox1.Text. Trim () + ". bak '";


2. Background

Using System. Data. SqlClient;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String SqlStr1 = "Server = (local); DataBase = master; Uid = sa; Pwd = ";
String SqlStr2 = "Exec sp_helpdb ";
SqlConnection con = new SqlConnection (SqlStr1 );
Con. Open ();
SqlCommand com = new SqlCommand (SqlStr2, con );
SqlDataReader dr = com. ExecuteReader ();
This. DropDownList1.DataSource = dr;
This. DropDownList1.DataTextField = "name ";
This. DropDownList1.DataBind ();
Dr. Close ();
Con. Close ();
}
}
Protected void button#click (object sender, EventArgs e)
{
String SqlStr1 = "Server = (local); database = '" + this. DropDownList1.SelectedValue + "'; Uid = sa; Pwd = ";
String SqlStr2 = "backup database" + this. DropDownList1.SelectedValue + "to disk = '" + this. TextBox1.Text. Trim () + ". bak '";
SqlConnection con = new SqlConnection (SqlStr1 );
Con. Open ();
Try
{
If (File. Exists (this. TextBox1.Text. Trim ()))
{
Response. Write ("");
Return;
}
SqlCommand com = new SqlCommand (SqlStr2, con );
Com. ExecuteNonQuery ();
Response. Write ("");
}
Catch (Exception error)
{
Response. Write (error. Message );
Response. Write ("");
}
Finally
{
Con. Close ();
}
}
}
Restore SqlServer
Core technologies:
String SqlStr1 = "Server = (local); database = '" + this. DropDownList1.SelectedValue + "'; Uid = sa; Pwd = ";
String SqlStr2 = "use master restore database" + dbname + "from disk = '" + path + "'";
1. Front-end
Operational Data Base

Operational Data Base


 

 
 

2. Background
Using System. Data. SqlClient;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String SqlStr1 = "Server = (local); DataBase = master; Uid = sa; Pwd = ";
String SqlStr2 = "Exec sp_helpdb ";
SqlConnection con = new SqlConnection (SqlStr1 );
Con. Open ();
SqlCommand com = new SqlCommand (SqlStr2, con );
SqlDataReader dr = com. ExecuteReader ();
This. DropDownList1.DataSource = dr;
This. DropDownList1.DataTextField = "name ";
This. DropDownList1.DataBind ();
Dr. Close ();
Con. Close ();
}
}
Protected void button#click (object sender, EventArgs e)
{
String path = this. FileUpload1.PostedFile. FileName; // obtain the backup path and database name.
String dbname = this. DropDownList1.SelectedValue;
String SqlStr1 = "Server = (local); database = '" + this. DropDownList1.SelectedValue + "'; Uid = sa; Pwd = ";
String SqlStr2 = "use master restore database" + dbname + "from disk = '" + path + "'";
SqlConnection con = new SqlConnection (SqlStr1 );
Con. Open ();
Try
{
SqlCommand com = new SqlCommand (SqlStr2, con );
Com. ExecuteNonQuery ();
Response. Write ("");
}
Catch (Exception error)
{
Response. Write (error. Message );
Response. Write ("");
}
Finally
{
Con. Close ();
}
}
}

Another method for backing up and restoring databases found on the Internet

. Net implements database backup and restoration to favorites

/// <Summary>
/// Back up the database
/// </Summary>
Private void btnBackUp_Click (object sender, System. EventArgs e)
{
This. Cursor = Cursors. WaitCursor;
This. label1.Text = "backing up data in the archive may take several seconds to dozens of times. Please wait ...";
This. label1.Visible = true;
This. label1.Refresh ();
This. pBar1.Visible = true;
// Configure //------------------------------------------------------------------------------------

String selfName = "D: NorthwindBak";
String deviceName = "NorthwindBak";
String remark = "backup test ";

// ◆ Data backup:
SQLDMO. Backup oBackup = new SQLDMO. BackupClass ();
SQLDMO. SQLServer oSQLServer = new SQLDMO. SQLServerClass ();
OBackup. Action = 0;
OBackup. Initialize = true;
SQLDMO. BackupSink_PercentCompleteEventHandler pceh = new SQLDMO. BackupSink_PercentCompleteEventHandler (Step );
OBackup. PercentComplete + = pceh;

Try
{
OSQLServer. LoginSecure = false;
OSQLServer. Connect (Common. MySettings. SqlServerName, "sa ","");
OBackup. Action = SQLDMO. SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
OBackup. Database = "Northwind"; // Database Name
OBackup. Files = selfName; // file path
OBackup. BackupSetName = deviceName; // backup name
OBackup. BackupSetDescription = remark; // backup description
OBackup. Initialize = true;
OBackup. SQLBackup (oSQLServer );

}
Catch (System. Exception ex)
{
Common. ShowMsg ("data backup failed: n" + ex. ToString ());
}
Finally
{
OSQLServer. DisConnect ();
}

// Configure //------------------------------------------------------------------------------------
This. label1.Visible = false;
This. pBar1.Visible = false;
This. Cursor = Cursors. Default;
}


/// <Summary>
/// Display the backup progress bar
/// </Summary>
Private void Step (string message, int percent)
{
This. pBar1.Value = percent;
}


Restoration Method

Data restoration:
/// <Summary>
/// Restore the database
/// </Summary>
Private void btnRestore_Click (object sender, System. EventArgs e)
{

This. Cursor = Cursors. WaitCursor;
This. label1.Text = "restoring the archive data may take several seconds to dozens of times. Please wait ...";
This. label1.Visible = true;
This. label1.Refresh ();
This. pBar1.Visible = true;
// Configure //------------------------------------------------------------------------------------

String fileName = "NorthwindBak ";
String filePath = "D: NorthwindBak ";
String remark = "backup test ";

SQLDMO. Restore oRestore = new SQLDMO. RestoreClass ();
SQLDMO. SQLServer oSQLServer = new SQLDMO. SQLServerClass ();
ORestore. Action = 0;
SQLDMO. RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO. RestoreSink_PercentCompleteEventHandler (Step );
ORestore. PercentComplete + = pceh;
Try
{
OSQLServer. Connect (Common. MySettings. SqlServerName, "sa ","");
SQLDMO. QueryResults qr = oSQLServer. EnumProcesses (-1 );
Int iColPIDNum =-1;
Int iColDbName =-1;

// Kill other connection Processes
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;
}

For (int I = 1; I <= qr. Rows; I ++)
{
Int lPID = qr. GetColumnLong (I, iColPIDNum );
String strDBName = qr. GetColumnString (I, iColDbName );
If (strDBName. ToUpper () = "CgRecord". ToUpper ())
OSQLServer. KillProcess (lPID );
}


ORestore. Action = SQLDMO. SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
ORestore. Database = "Northwind ";
ORestore. Files = filePath;
ORestore. FileNumber = 1;
ORestore. ReplaceDatabase = true;
ORestore. SQLRestore (oSQLServer );


}
Catch (System. Exception ex)
{
Common. ShowMsg ("data restoration failed: n" + ex. ToString ());
}
Finally
{
OSQLServer. DisConnect ();
}

// Configure //------------------------------------------------------------------------------------
This. label1.Visible = false;
This. pBar1.Visible = false;
This. Cursor = Cursors. Default;
}

/// <Summary>
/// Display the restore progress bar
/// </Summary>
Private void Step (string message, int percent)
{
This. pBar1.Value = percent;
}

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.