Data backup and restore under .NET

xiaoxiao2021-03-06  72

Database: SQL Server; Reference SQLDMO ◆ Data Backup: ///

/// Backup Database /// Private Void BtnBackup_Click (Object Sender, System.EventArgs E) {this.cursor = Cursors.Waitcursor; This.Label1.Text = "The data backup of the archive library can take a few seconds to dozens of time, please wait ..."; this.label1.visible = true; this.label1.refresh (); THIS.PBAR1.Visible = true; // ---------------------------------------- --------------------------------------------

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; // name of the backup oBackup.BackupSetDescription = remark; // backup description oBackup.Initialize = true; oBackup.SQLBackup (oSQLServer);} catch (System.Exception ex) { Common.showmsg ("Data Backup Failure: / N" EX.TOSTRING ());} finally {osqlser.disconnect ();}

/ / -------------------------------------------------------------------------------------------- ------------------------------------ this.Label1.visible = false; this.pbar1.visible = False; this.cursor = cursors.default;} ///

/// Display backup progress bar /// private void step (string message, int init percent) {this.pbar1.value = percent; } ◆ Data restore: /// /// Restore database /// private void btnrestore_click (object sender, system.eventargs e) {this.cursor = Cursors.waitcursor; this.label1.text = "The data restore of the archive library, this may take a few seconds to dozens of time, please wait ..."; this.label1.visible = true; this.label1.refresh (); this.pbar1.visible = True; // -------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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 = osqlser.enumprocesses (-1); int icolpidnum = -1; int icholdbname = -1;

// Kill other connection processes for (int i = 1; i

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 Restore Failure: / N" EX.TOSTRING ());} finally {osqlser.disconnect ();} // --------- -------------------------------------------------- ------------------------- this.label1.visible = false; this.pbar1.visible = false; this.cursor = cursors.default;

转载请注明原文地址:https://www.9cbs.com/read-91594.html

New Post(0)