Asp.net| Backup | recovery | data | database | page using System;
Using SQLDMO;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Namespace bakServe040907
{
<summary>
Database Backup class Description
</summary>
public class Bakserver
{
String ServerName; Data server name
String UserName; User name
String Password; User password
String message; Message prompt
ProgressBar Pbar; Status bar control
Public Bakserver ()
{
}
<summary>
Get List of database servers
</summary>
<returns> Database Server List </returns>
Public ArrayList getserverlist ()
{
ArrayList alservers = new ArrayList ();
SQLDMO. Application Sqlapp = new SQLDMO. ApplicationClass ();
Try
{
SQLDMO. NameList serverlist = Sqlapp.listavailablesqlservers ();
for (int i = 1;i<= serverlist.count;i++)
{
Alservers.add (Serverlist.item (i));
}
}
catch (Exception e)
{
Throw (New Exception ("Fetch database server List error:" +e.message));
message = "Fetch database server List error:" +e.message;
}
Finally
{
Sqlapp.quit ();
}
return alservers;
}
<summary>
Error message Handling
</summary>
<returns> Message Information </returns>
public string MSG ()
{
return message;
}
<summary>
Get the list of specified databases
</summary>
<param name= "strServerName" > Server name </param>
<param name= "strUserName" > User name </param>
<param name= "strpwd" > User password </param>
<returns> Database List </returns>
Public ArrayList getdblist (string strservername,string strusername,string strpwd)
{
ServerName = strServerName;
UserName = strUserName;
Password = strpwd;
ArrayList Aldbs = new ArrayList ();
SQLDMO. Application Sqlapp = new SQLDMO. ApplicationClass ();
SQLDMO. SQL Server 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 ("Connection Database error:" +e.message));
message = "Connection Database error:" +err. message;
}
Finally
{
Svr. DisConnect ();
Sqlapp.quit ();
}
return Aldbs;
}
Database backup and real-time progress display code:
<summary>
Database backup and Live progress display
</summary>
<param name= "strDbName" > Database name </param>
<param name= "strFileName" > backup file name </param>
<param name= "Pgbmain" > Status bar control name </param>
<param name= "strServerName" > Server name </param>
<param name= "strUserName" > User name </param>
<param name= "strpwd" > Password </param>
<returns> backup successfully returns TRUE, otherwise returns false</returns>
public bool BackupDB (string strdbname,string strfilename, ProgressBar pgbmain,string strservername,string Strusername,string strpwd)
{
ServerName = strServerName;
UserName = strUserName;
Password = strpwd;
Pbar = Pgbmain;
SQLDMO. SQL Server 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;
<summary>
Displays the current progress of the progress bar
</summary>
<param name= "Message" ></param>
<param name= "percent" ></param>
private void step (string message,int percent)
{
Pbar.value = percent;
}
Where these two statements achieve the real-time display of progress:
SQLDMO. Backupsink_percentcompleteeventhandler PCEH = new SQLDMO. Backupsink_percentcompleteeventhandler (step);
Bak. PercentComplete + = PCEH;
Step is the name of the method above private void step (string message,int percent), which is used to display the current progress of the progress bar.
<summary>
Database recovery and kill process
</summary>
<param name= "strDbName" > Database name </param>
<param name= "strFileName" > backup file name </param>
<param name= "Pgbmain" > Status bar control name </param>
<param name= "strServerName" > Server name </param>
<param name= "strUserName" > User name </param>
<param name= "strpwd" > Password </param>
<returns> restore successfully returns TRUE, otherwise return false</returns>
public bool RestoreDB (string strdbname,string strfilename, ProgressBar pgbmain,string strservername,string Strusername,string strpwd)
{
Pbar = Pgbmain;
SQLDMO. SQL Server 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)
{
Throw ("Restore the database failed, please close all the programs that connect to the database!") Exception "+err. message));
message = Restore Database failed, close all programs that connect to the database! "+err. message;
return false;
}
Finally
{
Svr. DisConnect ();
}
}
}
}
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.