Sqldmo backup to restore the sqlserver2000 Database

Source: Internet
Author: User
Private void btnBackUp_Click (object sender, System. EventArgs e)
{
If (rtbBack. Text. Trim () = string. Empty)
{
MessageBox. Show ("Remarks required", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Asterisk );
}
Else
{
This. Cursor = Cursors. WaitCursor;
This. lblTitle. Text = "backing up data in the archives may take several seconds to dozens of times. Please wait ...";
This. lblTitle. Visible = true;
This. lblTitle. Refresh ();
This. pBar1.Visible = true;
// Configure //------------------------------------------------------------------------------------

String strCurrentyTime = DateTime. Now. ToString ();
String strFileName = "LibraryBookDB" + strCurrentyTime. toString (). replace ("-",""). replace (":",""). replace ("", "") + ". bak ";
String strFilePath = Application. StartupPath + ConfigurationSettings. deleetpath ["strFilePath"] + strFileName;
String deviceName = "LibraryBookDB. Bak ";
String remark = "backup test ";


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 ","");
OSQLServer. Connect (ConfigurationSettings. deleettings ["strServerName"], "sa ","");
OBackup. Action = SQLDMO. SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
OBackup. Database = ConfigurationSettings. deleettings ["strDBName"]; // Database Name
OBackup. Files = strFilePath; // 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:" + ex. ToString ());
MessageBox. Show ("data backup failed:" + ex. ToString ());
Return;
}
Finally
{
OSQLServer. DisConnect ();
}
// Configure //-----------------------------------------------------------------------------------
// Write the xml file
DBBackUpAndRestoreClass myDBBackUpAndRestoreClass = new DBBackUpAndRestoreClass ();
MyDBBackUpAndRestoreClass. InsertXmlFile (strFileName, ConfigurationSettings. receivettings ["strFilePath"], strCurrentyTime, rtbBack. Text );
// Display it in the datagrid Control
FrmDBBackUpAndRestore_Load (sender, e );
// Configure //------------------------------------------------------------------------------------
This. lblTitle. Visible = false;
This. pBar1.Visible = false;
This. Cursor = Cursors. Default;
This. rtbBack. Text = string. Empty;
This. rtbBack. Focus ();
}
}

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

Private void btnRestore_Click (object sender, System. EventArgs e)
{
If (MessageBox. Show ("are you sure you want to restore the database? "," Prompt ", MessageBoxButtons. YesNo, MessageBoxIcon. Question) = DialogResult. Yes)
{
If (dgrDataShow. IsSelected (dgrDataShow. CurrentRowIndex ))
{
DataRow myDataRow = (DataTable) (dgrDataShow. DataSource). Rows [dgrDataShow. CurrentRowIndex];
This. Cursor = Cursors. WaitCursor;
This. lblTitle. Text = "restoring the archive data. It may take several seconds to dozens of times. Please wait ...";
This. lblTitle. Visible = true;
This. lblTitle. Refresh ();
This. pBar1.Visible = true;
// Configure //------------------------------------------------------------------------------------

String strFileName = myDataRow ["FileName"]. ToString ();
// String strFilePath = Application. StartupPath + myDataRow ["file path"]. ToString () + strFileName;
String strFilePath = myDataRow ["FilePath"]. ToString ();
String strTureFilePath = Application. StartupPath + strFilePath + strFileName;

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 (ConfigurationSettings. deleettings ["strServerName"], "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 () = ConfigurationSettings. Fig ["strDBName"]. ToString (). ToUpper ())
OSQLServer. KillProcess (lPID );
}

ORestore. Action = SQLDMO. SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
ORestore. Database = ConfigurationSettings. receivettings ["strDBName"]. ToString (); // Database Name
ORestore. Files = strTureFilePath;
ORestore. FileNumber = 1;
ORestore. ReplaceDatabase = true;
ORestore. SQLRestore (oSQLServer );
MessageBox. Show ("the database is successfully restored and the software is restarted. "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Asterisk );
// Exit the program
Application. Exit ();
// Restart the program
Process. Start ("LibraryBookManageSystem.exe ");

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

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

}
Else
{
MessageBox. Show ("select the database backup to be restored", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Asterisk );
}
}
}

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.