zoukankan      html  css  js  c++  java
  • C#中数据库备份还原

    使用前要导入SQLDMO.dll(在com组件中导入Microsoft SQLDMO Object Library即可)

    /// 
        /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
        /// 
        public sealed class DbOper
        {
            /// 
            /// DbOper类的构造函数
            /// 
            public DbOper()
            {
            }
            /// 
            /// 数据库备份
            /// 
            public static void DbBackup()
            {
                SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
                SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
                try
                {
                    oSQLServer.LoginSecure = false;
                    oSQLServer.Connect(".","sa","sasa");//服务器名、账号、密码
                    oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                    oBackup.Database = "tian";
                    oBackup.Files = @"d:\tian.bak";
                    oBackup.BackupSetName = "tian";
                    oBackup.BackupSetDescription = "数据库备份";
                    oBackup.Initialize = true;
                    oBackup.SQLBackup(oSQLServer);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    oSQLServer.DisConnect();
                }
            }
            /// 
            /// 数据库恢复
            /// 
    
            public static void DbRestore()
            {
                SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
                SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
                try
                {
                    oSQLServer.LoginSecure = false;
                    oSQLServer.Connect(".", "sa", "sasa");
                    oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                    oRestore.Database = "tian";
                    oRestore.Files = @"d:tian.bak";
                    oRestore.FileNumber = 1;
                    oRestore.ReplaceDatabase = true;
                    oRestore.SQLRestore(oSQLServer);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    oSQLServer.DisConnect();
                }
            }
        }


    要对SQLDMO.dll一个属性改为false   否则会报错

  • 相关阅读:
    spring mvc + kafka实战
    springboot 实现文件下载功能
    vue前端文件下载
    父类和子类初始化顺序
    几种单例模式
    全链路压测注意点
    压力测试-ab
    压力测试-locust讲解
    Java httpClient 中get, post ,put(form-data & raw), delete方法使用
    RequestBody 和RequestEntity使用
  • 原文地址:https://www.cnblogs.com/wujy/p/2932671.html
Copyright © 2011-2022 走看看