首页 »DotNet » sqldmo.sqlserver:C#中结合使用SQLDMO实现备份、还原SQLserver数据库 »正文
sqldmo.sqlserver:C#中结合使用SQLDMO实现备份、还原SQLserver数据库
来源: 发布时间:星期五, 2009年1月9日 浏览:59次 评论:0
using; using.Collections.Generic; using.ComponentModel; using.Data; using.Drawing; using.Text; using.Windows.Forms; Magicbit.Framework { publicpartialDBTools:Form { privateDBTools_Instance=null; publicDBToolsInstance { (_Instancenull) { _Instance=DBTools; } { MessageBox.Show("已经有个例子在运行!"); } _Instance; } publicDBTools { InitializeComponent; } privatevoidBackAndRecoverDB_Load(objectsender,EventArgse) { this.txtSavePath.Text=Application.StartupPath; //this.GetSQLServerList; } privatevoidGetSQLServerList { //getallavailableSQLServers SQLDMO._ApplicationsqlApp=SQLDMO.ApplicationClass; SQLDMO.NameListsqlServers=sqlApp.ListAvailableSQLServers; for(i=0;i<sqlServers.Count;i) { objectsrv=sqlServers.Item(i+1); (srv!=null) { this.cboServers.Items.Add(srv); } } (this.cboServers.Items.Count>0) this.cboServers.SelectedIndex=0; this.cboServers.Text="<NoavailableSQLServers>"; } privatevoidGetBackUpDB { SQLDMO.ApplicationsqlApp =SQLDMO.ApplicationClass; SQLDMO.SQLServersrv =SQLDMO.SQLServer; srv.Connect(this.cboServers.Text.Trim,this.txtUserName.Text.Trim,this.txtPassword.Text.Trim); foreach(SQLDMO.Databasedbinsrv.Databases) { (db.Name!=null) this.cboDatabase.Items.Add(db.Name); } } privatevoidpictureBox1_Click(objectsender,EventArgse) { MessageBox.Show("欢迎使用数据库备份、还原工具本工具将协助你备份和还原数据库确保数据安全!","备份您数据库"); } privatevoidbutton1_Click(objectsender,EventArgse) { this.GetBackUpDB; } privatevoidBackUpDB { selfName=this.txtSavePath.Text.Trim+@""+this.cboDatabase.Text.Trim+"_"+.DateTime.Now.("yyyyMMddHHmmss")+".DAT"; deviceName=this.cboDatabase.Text.Trim+"bak"; remark="数据备份"; //BACKUPDB SQLDMO.BackupoBackup=SQLDMO.BackupClass; SQLDMO.SQLServeroSQLServer=SQLDMO.SQLServerClass; oBackup.Action=0; oBackup.Initialize=true; SQLDMO.BackupSink_PercentCompleteEventHandlerpceh=SQLDMO.BackupSink_PercentCompleteEventHandler(Step); oBackup.PercentCompletepceh; 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;//备份名称 oBackup.BackupSetDescription=remark;//备份描述 oBackup.Initialize=true; oBackup.SQLBackup(oSQLServer); } catch(.Exceptionex) { MessageBox.Show("数据备份失败: "+ex.); } finally { oSQLServer.DisConnect; } } privatevoidStep(message,percent) { 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 { filePath=this.txtBackUpFile.Text.Trim; SQLDMO.RestoreoRestore=SQLDMO.RestoreClass; SQLDMO.SQLServeroSQLServer=SQLDMO.SQLServerClass; oRestore.Action=0; SQLDMO.RestoreSink_PercentCompleteEventHandlerpceh=SQLDMO.RestoreSink_PercentCompleteEventHandler(Step); oRestore.PercentCompletepceh; try { oSQLServer.Connect(this.cboServers.Text.Trim,this.txtUserName.Text.Trim,this.txtPassword.Text.Trim); SQLDMO.QueryResultsqr=oSQLServer.EnumProcesses(-1); iColPIDNum=-1; iColDbName=-1; //杀死其它连接进程 for(i=1;i<=qr.Columns;i) { strName=qr.get_ColumnName(i); (strName.ToUpper.Trim"SPID") { iColPIDNum=i; } (strName.ToUpper.Trim"DBNAME") { iColDbName=i; } (iColPIDNum!=-1&&iColDbName!=-1) ; } for(i=1;i<=qr.Rows;i) { lPID=qr.GetColumnLong(i,iColPIDNum); strDBName=qr.GetColumnString(i,iColDbName); (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(.Exceptionex) { MessageBox.Show("数据还原失败: "+ex.); } 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(objectsender,EventArgse) { this.Cursor=Cursors.WaitCursor; this.label6.Visible=true; this.progressBar1.Visible=true; this.RestoreDB; this.Cursor=Cursors.Default; this.label6.Text="还原已完成."; } } }
相关文章
读者评论
发表评论
|
|