. Data backup and restore under net

Source: Internet
Author: User
Tags connect sql net tostring trim backup

databases: SQL Server; referencing SQLDMO

Data backup:
<summary>
Backing up a database
</summary>
private void Btnbackup_click (object sender, System.EventArgs e)
{
This. Cursor = Cursors.waitcursor;
This.label1.Text = "A data backup of the archive is in progress, which may take a few seconds to dozens of, please wait a moment ...";
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. SQL Server 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 ();
  }

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


<summary>
Show Backup progress bar
</summary>
private void step (string message,int percent)
{
This.pBar1.Value = percent;
}

Data restore:

<summary>
Restore Database
</summary>
private void Btnrestore_click (object sender, System.EventArgs e)
{

This. Cursor = Cursors.waitcursor;
This.label1.Text = "The data restore in the archive is in progress, this may take a few seconds to dozens of, please wait a moment ...";
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. SQL Server 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 the other connection process
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 () = = "Northwind". 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 restore failed: \ n" + ex.) ToString ());
  }
   finally
   {
    osqlserver.disconnect ();
  }
 
  //--------------------------------------------------------------------- ---------------
   this.label1.Visible = false;
   this.pBar1.Visible = false;
   this. Cursor = Cursors.Default;
 }

<summary>
Show Restore progress bar
</summary>
private void step (string message,int percent)
{
This.pBar1.Value = percent;
}



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.