大众计算机学习网

专门提供各种电脑教程

ASP.NET利用SQLDMO实现数据库的备份与恢复上篇

Admin | 2008-1-13 18:50:56 | 被阅次数 | 5402

        ①:添加组件
 
       
打开阁下的VS2005,执行如下操作:
 
        “网站”→“添加引用”;弹出“添加引用”对话框,切换到“COM”选项卡,在组件名称中找到“Microsoft   SQLDMO   Object   Library”,点右边的“选择”按钮进行添加!!!

        ②:实现数据库的备份代码:

        public bool DbBackup()
         {
            string path = CreatePath();
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(server,uid, pwd);
                oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                oBackup.Database = database;
                oBackup.Files = path;
                oBackup.BackupSetName = database;
                oBackup.BackupSetDescription = "数据库备份";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);

                return true;
            }
            catch(Exception ex)
            {
                return false;
                throw ex;
            }
            finally
            {
                oSQLServer.DisConnect();
            }
        }

下一篇将提供恢复数据库代码