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");//服务器名、账号、密码  服务器名:ADMIN-PCMGX2005、192.168.0.111sql2005 有时候需要加SQL2005实例
                    oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                    oBackup.Database = "tian";
                    oBackup.Files = @"d: ian.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();
                }
            }
        }

     
     
  • 相关阅读:
    许可和授权的研究及其破解
    Citect:How do I translate Citect error messages?
    异步IO模型和Overlapped结构
    SanDisk SecureAccess™ Software
    Asynchronous socket communication
    一种字节转字符串的语法
    【转载】C# Tutorial
    保存项目文件“XXX.csprj”时出错。类没有注册。
    markdown中的注释
    ubuntu上nginx详细安装部署教程
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3339887.html
Copyright © 2011-2022 走看看