• ADO.NET 数据库备份等操作



    public class SqlServerBackup { private string database; private string server; private string uid; private string pwd; public string Database { get { return this.database; } set { this.database = value; } } public string Server { get { return this.server; } set { this.server = value; } } public string Pwd { get { return this.pwd; } set { this.pwd = value; } } public string Uid { get { return this.uid; } set { this.uid = value; } } public bool DbBackup(string url) { Backup oBackup = new BackupClass(); SQLServer oSQLServer = new SQLServerClass(); bool result; try { oSQLServer.LoginSecure = false; oSQLServer.Connect(this.server, this.uid, this.pwd); oBackup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; oBackup.Database = this.database; oBackup.Files = url; oBackup.BackupSetName = this.database; oBackup.BackupSetDescription = "数据库备份"; oBackup.Initialize = true; oBackup.SQLBackup(oSQLServer); result = true; } catch { result = false; } finally { oSQLServer.DisConnect(); } return result; } public bool DbRestore(string url) { bool result; if (!this.exepro()) { result = false; } else { Restore oRestore = new RestoreClass(); SQLServer oSQLServer = new SQLServerClass(); try { oSQLServer.LoginSecure = false; oSQLServer.Connect(this.server, this.uid, this.pwd); oRestore.Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; oRestore.Database = this.database; oRestore.Files = url; oRestore.FileNumber = 1; oRestore.ReplaceDatabase = true; oRestore.SQLRestore(oSQLServer); result = true; } catch { result = false; } finally { oSQLServer.DisConnect(); } } return result; } private bool exepro() { SqlConnection conn = new SqlConnection(string.Concat(new string[] { "server=", this.server, ";uid=", this.uid, ";pwd=", this.pwd, ";database=master" })); SqlCommand cmd = new SqlCommand("killspid", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@dbname", this.database); bool result; try { conn.Open(); cmd.ExecuteNonQuery(); result = true; } catch { result = false; } finally { conn.Close(); } return result; } }

    只需引入Interop.SQLDMO.dll

  • 相关阅读:
    Linux c++ 试验4 一个输出整形类型数值问题
    Linux c++ 试验6 右值
    vi 常用命令
    ensp学习1 ensp无法运行启动问题解决
    Linux c++ 试验7 函数里定义的string不能返回成指针
    ensp学习2 ensp设置接口ip
    mmcblk0p0和sda1等等的含义
    大白话解释一下什么是NFT
    Docker实现Mysql主从复制
    docker安装redis
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/6846069.html
Copyright © 2020-2023  润新知