Database backup and recovery in asp.net

Source: Internet
Author: User

WebDataBak. aspx. cs

BakServer. cs is the custom class code

Core code:

1.

Private void BtnDataBackup_Click (object sender, System. EventArgs e)
{
If (lstDb. Items. Count = 0)
{
Pub. Alert ("the Database List cannot be blank! ", This. Page );
}
Else if (txtInFile. Text = "")
{
Pub. Alert ("the backup file name cannot be blank! ", This. Page );

}
Else if (txtDbName. Text = "" | txtUserName. Text = "")
{
Pub. Alert ("database, user cannot be blank !! ", This. Page );

}
Else
{
BtnDataRestore. Enabled = false;
BtnQuery. Enabled = false;
BtnCear. Enabled = false;
If (baks. BackUPDB (lstDb. SelectedItem. ToString (), txtInFile. Text, txtDbName. Text, txtUserName. Text, txtPwd. Text) = true)
{
Pub. Alert ("backup successful! ", This. Page );
}
Else
{
Pub. Alert ("The path to be backed up does not exist. Please enter the correct path !! ", This. Page );
}
BtnDataRestore. Enabled = true;
BtnQuery. Enabled = true;
BtnCear. Enabled = true;
}
}

Private void BtnDataRestore_Click (object sender, System. EventArgs e)
{
// Restore the database
If (lstDb. Items. Count = 0)
{
Pub. Alert ("the Database List cannot be blank! ", This. Page );
}
Else if (txtOutFile. Text = "")
{
Pub. Alert ("the name of the restored file cannot be blank! ", This. Page );

}
Else if (txtDbName. Text = "" | txtUserName. Text = "")
{
Pub. Alert ("database, user cannot be blank !! ", This. Page );

}
Else
{
BtnDataBackup. Enabled = false;
BtnQuery. Enabled = false;
BtnCear. Enabled = false;

If (baks. RestoreDB (lstDb. SelectedItem. ToString (), txtOutFile. Text, txtDbName. Text, txtUserName. Text, txtPwd. Text) = true)
{
Pub. Alert ("recovery successful! ", This. Page );
}
Else
{
Pub. Alert ("the database file to be restored does not exist. Enter the correct path !! ", This. Page );
}
BtnDataBackup. Enabled = true;
BtnQuery. Enabled = true;
BtnCear. Enabled = true;
}
}

2.

Namespace WebSearch
{
///

/*--------------------------------------------------------------------------
System name: xxxxx

Design Date:
Code designer: winzheng
Module name: Backup and restoration of sqlserver 2000 database under asp.net
Module ID: DbBAK
--------------------------------------------------------------------------
*/
Public class bakServer
{
String ServerName; // data server name
String UserName; // User Name
String Password; // User Password
String message; // message prompt

Public bakServer ()
{
//
// TODO: add the constructor logic here
//
}
///

/// Obtain the database server list
///
/// Database Server LIST
Public ArrayList GetServerList ()
{
ArrayList alServers = new ArrayList ();
SQLDMO. ApplicationClass sqlApp = new SQLDMO. ApplicationClass ();
Try
{
SQLDMO. NameList serverList = sqlApp. ListAvailableSQLServers ();
If (serverList! = Null)
{
For (int I = 1; I <= serverList. Count; I ++)
{
AlServers. Add (serverList. Item (I ));
}
}
Else
{
Message = "your system needs to be patched with SQL SERVER 2000 SP3 ";
}
}
Catch (Exception e)
{

Message = "An error occurred while retrieving the Database Server LIST:" + e. Message;

}
Finally
{
SqlApp. Quit ();
}
Return alServers;
}
///

/// Handle error messages
///
/// Message Information
Public string Msg ()
{
Return message;
}
///

/// Obtain the specified database list
///
///Server Name
///User Name
///User Password
/// Database List
Public ArrayList GetDbList (string strServerName, string strUserName, string strPwd)
{
ServerName = strServerName;
UserName = strUserName;
Password = strPwd;

ArrayList alDbs = new ArrayList ();
SQLDMO. ApplicationClass sqlApp = new SQLDMO. ApplicationClass ();
SQLDMO. svr = new SQLDMO. SQLServerClass ();
Try
{
Svr. Connect (ServerName, UserName, Password );
Foreach (SQLDMO. Database db in svr. Databases)
{
If (db. Name! = Null)
AlDbs. Add (db. Name );
}
}
Catch (Exception err)
{
// Throw (new Exception ("database connection error:" + e. Message ));
Message = "database connection error:" + err. Message;
}
Finally
{
Svr. DisConnect ();
SqlApp. Quit ();
}
Return alDbs;
}
///
///Database Name
///Backup File Name
///Server Name
///User Name
///Password
/// Returns true if the backup is successful; otherwise, returns false.
Public bool BackUPDB (string strDbName, string strFileName, string strServerName, string strUserName, string strPwd)
{
ServerName = strServerName;
UserName = strUserName;
Password = strPwd;
SQLDMO. svr = new SQLDMO. SQLServerClass ();
Try
{
Svr. Connect (ServerName, UserName, Password );
SQLDMO. Backup bak = new SQLDMO. BackupClass ();
Bak. Action = 0;
Bak. Initialize = true;
SQLDMO. BackupSink_PercentCompleteEventHandler pceh = new SQLDMO. BackupSink_PercentCompleteEventHandler (Step );
Bak. PercentComplete + = pceh;
Bak. Files = strFileName;
Bak. Database = strDbName;
Bak. SQLBackup (svr );
Return true;
}
Catch (Exception err)
{
// Throw (new Exception ("failed to back up the database" + err. Message ));
Message = "failed to back up database:" + err. Message;
Return false;
}
Finally
{
Svr. DisConnect ();
}
}

///Backup File Name
///Status Bar Control name
///Server Name
///User Name
///Password
/// Returns true if the restoration is successful. Otherwise, returns false.
Public bool RestoreDB (string strDbName, string strFileName, string strServerName, string strUserName, string strPwd)
{
SQLDMO. svr = new SQLDMO. SQLServerClass ();
Try
{
ServerName = strServerName;
UserName = strUserName;
Password = strPwd;

Svr. Connect (ServerName, UserName, Password );
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;
}

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;
SQLDMO. RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO. RestoreSink_PercentCompleteEventHandler (Step );
Res. PercentComplete + = pceh;
Res. Files = strFileName;

Res. Database = strDbName;
Res. ReplaceDatabase = true;
Res. SQLRestore (svr );
Return true;
}
Catch (Exception err)
{

Message = "failed to restore database. Please close all programs connected to the database! "+ Err. Message;
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.