• 如何远程备份sql server数据库


    方法一(不使用SQLDMO):

    ///
    ///备份方法
    ///
    SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=sa;");

    SqlCommand cmdBK = new SqlCommand();
    cmdBK.CommandType = CommandType.Text;
    cmdBK.Connection = conn;
    cmdBK.CommandText = @"backup database test to disk='C:\ba' with init";

    try
    {
    conn.Open();
    cmdBK.ExecuteNonQuery();
    MessageBox.Show("Backup successed.");
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    conn.Close();
    conn.Dispose();
    }


    ///
    ///还原方法
    ///
    SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=sa;Trusted_Connection=False");
    conn.Open();

    //KILL DataBase Process
    SqlCommand cmd = new SqlCommand("SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='test'", conn);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    ArrayList list = new ArrayList();
    while(dr.Read())
    {
    list.Add(dr.GetInt16(0));
    }
    dr.Close();
    for(int i = 0; i < list.Count; i++)
    {
    cmd = new SqlCommand(string.Format("KILL {0}", list[i]), conn);
    cmd.ExecuteNonQuery();
    }

    SqlCommand cmdRT = new SqlCommand();
    cmdRT.CommandType = CommandType.Text;
    cmdRT.Connection = conn;
    cmdRT.CommandText = @"restore database test from disk='C:\ba'";

    try
    {
    cmdRT.ExecuteNonQuery();
    MessageBox.Show("Restore successed.");
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    conn.Close();
    }

    方法二(使用SQLDMO):

    ///
    ///备份方法
    ///
    SQLDMO.Backup backup = new SQLDMO.BackupClass();
    SQLDMO.SQLServer server = new SQLDMO.SQLServerClass();
    //显示进度条
    SQLDMO.BackupSink_PercentCompleteEventHandler progress = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
    backup.PercentComplete += progress;

    try
    {
    server.LoginSecure = false;
    server.Connect(".", "sa", "sa");
    backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
    backup.Database = "test";
    backup.Files = @"D:\test\myProg\backupTest";
    backup.BackupSetName = "test";
    backup.BackupSetDescription = "Backup the database of test";
    backup.Initialize = true;
    backup.SQLBackup(server);
    MessageBox.Show("Backup successed.");
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    server.DisConnect();
    }
    this.pbDB.Value = 0;

    ///
    ///还原方法
    ///
    SQLDMO.Restore restore = new SQLDMO.RestoreClass();
    SQLDMO.SQLServer server = new SQLDMO.SQLServerClass();
    //显示进度条
    SQLDMO.RestoreSink_PercentCompleteEventHandler progress = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
    restore.PercentComplete += progress;

    //KILL DataBase Process
    SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=sa;Trusted_Connection=False");
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='test'", conn);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    ArrayList list = new ArrayList();
    while(dr.Read())
    {
    list.Add(dr.GetInt16(0));
    }
    dr.Close();
    for(int i = 0; i < list.Count; i++)
    {
    cmd = new SqlCommand(string.Format("KILL {0}", list[i]), conn);
    cmd.ExecuteNonQuery();
    }
    conn.Close();

    try
    {
    server.LoginSecure = false;
    server.Connect(".", "sa", "sa");
    restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
    restore.Database = "test";
    restore.Files = @"D:\test\myProg\backupTest";
    restore.FileNumber = 1;
    restore.ReplaceDatabase = true;
    restore.SQLRestore(server);
    MessageBox.Show("Restore successed.");
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    server.DisConnect();
    }
    this.pbDB.Value = 0;



    本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!
    http://www.studycs.com/ShowArticle.aspx?id=538
  • 相关阅读:
    Windows 10 搭建Python3 安装使用 protobuf
    [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
    [Python爬虫] 在Windows下安装PIP+Phantomjs+Selenium
    [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图
    [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
    [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
    [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
    [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
    [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈
    [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍
  • 原文地址:https://www.cnblogs.com/draeag/p/834481.html
Copyright © 2020-2023  润新知