usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceMagicbit.Framework
{
publicpartialclassDBTools:Form
{
privatestaticDBTools_Instance=null;
publicstaticDBToolsInstance()
{
if(_Instance==null)
{
_Instance=newDBTools();
}
else
{
MessageBox.Show("已经有一个实例在运行!");
}
return_Instance;
}
publicDBTools()
{
InitializeComponent();
}
privatevoidBackAndRecoverDB_Load(objectsender,EventArgse)
{
this.txtSavePath.Text=Application.StartupPath;
//this.GetSQLServerList();
}
privatevoidGetSQLServerList()
{
//getallavailableSQLServers
SQLDMO._ApplicationsqlApp=newSQLDMO.ApplicationClass();
SQLDMO.NameListsqlServers=sqlApp.ListAvailableSQLServers();
for(inti=0;i<sqlServers.Count;i++)
{
objectsrv=sqlServers.Item(i+1);
if(srv!=null)
{
this.cboServers.Items.Add(srv);
}
}
if(this.cboServers.Items.Count>0)
this.cboServers.SelectedIndex=0;
else
this.cboServers.Text="<NoavailableSQLServers>";
}
privatevoidGetBackUpDB()
{
SQLDMO.ApplicationsqlApp =newSQLDMO.ApplicationClass();
SQLDMO.SQLServersrv =newSQLDMO.SQLServer();
srv.Connect(this.cboServers.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassword.Text.Trim());
foreach(SQLDMO.Databasedbinsrv.Databases)
{
if(db.Name!=null)
this.cboDatabase.Items.Add(db.Name);
}
}
privatevoidpictureBox1_Click(objectsender,EventArgse)
{
MessageBox.Show("欢迎使用数据库备份、还原工具,本工具将协助你备份和还原数据库,确保数据安全!","备份您的数据库");
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
this.GetBackUpDB();
}
privatevoidBackUpDB()
{
stringselfName=this.txtSavePath.Text.Trim()+@""+this.cboDatabase.Text.Trim()+"_"+System.DateTime.Now.ToString("yyyyMMddHHmmss")+".DAT";
stringdeviceName=this.cboDatabase.Text.Trim()+"bak";
stringremark="数据备份";
//BACKUPDB
SQLDMO.BackupoBackup=newSQLDMO.BackupClass();
SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
oBackup.Action=0;
oBackup.Initialize=true;
SQLDMO.BackupSink_PercentCompleteEventHandlerpceh=newSQLDMO.BackupSink_PercentCompleteEventHandler(Step);
oBackup.PercentComplete+=pceh;
try
{
oSQLServer.LoginSecure=false;
oSQLServer.Connect(this.cboServers.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassword.Text.Trim());
oBackup.Action=SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database=this.cboDatabase.Text.Trim();//数据库名
oBackup.Files=selfName;//文件路径
oBackup.BackupSetName=deviceName;//备份名?br /> oBackup.BackupSetDescription=remark;//备份描述
oBackup.Initialize=true;
oBackup.SQLBackup(oSQLServer);
}
catch(System.Exceptionex)
{
MessageBox.Show("数据备份失败: "+ex.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
privatevoidStep(stringmessage,intpercent)
{
this.progressBar1.Value=percent;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
this.Cursor=Cursors.WaitCursor;
this.label6.Visible=true;
this.progressBar1.Visible=true;
this.BackUpDB();
this.Cursor=Cursors.Default;
this.label6.Text="备份已完成.";
}
publicvoidRestoreDB()
{
stringfilePath=this.txtBackUpFile.Text.Trim();
SQLDMO.RestoreoRestore=newSQLDMO.RestoreClass();
SQLDMO.SQLServeroSQLServer=newSQLDMO.SQLServerClass();
oRestore.Action=0;
SQLDMO.RestoreSink_PercentCompleteEventHandlerpceh=newSQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
oRestore.PercentComplete+=pceh;
try
{
oSQLServer.Connect(this.cboServers.Text.Trim(),this.txtUserName.Text.Trim(),this.txtPassword.Text.Trim());
SQLDMO.QueryResultsqr=oSQLServer.EnumProcesses(-1);
intiColPIDNum=-1;
intiColDbName=-1;
//杀死其它的连接进程
for(inti=1;i<=qr.Columns;i++)
{
stringstrName=qr.get_ColumnName(i);
if(strName.ToUpper().Trim()=="SPID")
{
iColPIDNum=i;
}
elseif(strName.ToUpper().Trim()=="DBNAME")
{
iColDbName=i;
}
if(iColPIDNum!=-1&&iColDbName!=-1)
break;
}
for(inti=1;i<=qr.Rows;i++)
{
intlPID=qr.GetColumnLong(i,iColPIDNum);
stringstrDBName=qr.GetColumnString(i,iColDbName);
if(strDBName.ToUpper()=="CgRecord".ToUpper())
oSQLServer.KillProcess(lPID);
}
oRestore.Action=SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database=this.cboDBtoBackup.Text;
oRestore.Files=filePath;
oRestore.FileNumber=1;
oRestore.ReplaceDatabase=true;
oRestore.SQLRestore(oSQLServer);
}
catch(System.Exceptionex)
{
MessageBox.Show("数据还原失败: "+ex.ToString());
}
finally
< {
oSQLServer.DisConnect();
}
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
this.folderBrowserDialog1.Description="请选择备份文件存放目录";
this.folderBrowserDialog1.ShowNewFolderButton=true;
this.folderBrowserDialog1.ShowDialog();
this.txtSavePath.Text=this.folderBrowserDialog1.SelectedPath;
}
privatevoidbutton4_Click(objectsender,EventArgse)
{
this.openFileDialog1.DefaultExt="*.dat";
this.openFileDialog1.Title="请选择要还原的数据库备份文件.";
this.openFileDialog1.ShowDialog();
this.txtBackUpFile.Text=this.openFileDialog1.FileName;
}
privatevoidbutton5_Click(obje ctsender,EventArgse)
{
this.Cursor=Cursors.WaitCursor;
this.label6.Visible=true;
this.progressBar1.Visible=true;
this.RestoreDB();
this.Cursor=Cursors.Default;
this.label6.Text="还原已完成.";
}
}
}
版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明!