zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    BZOJ1212 [HNOI2004]L语言
    BZOJ1202 [HNOI2005]狡猾的商人
    BZOJ1295 [SCOI2009]最长距离
    BZOJ1266 [AHOI2006]上学路线
    BZOJ1297 [SCOI2009]迷路
    php declare (ticks = N)
    使用 trait 时报PHP Parse error: syntax error, unexpected 'use' (T_USE) 这个错误
    yii2 and short_open_tag
    Nginx负载均衡配置实例详解
    Could not fetch https://api.github.com/repos/RobinHerbots/jquery
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/6846069.html
Copyright © 2011-2022 走看看