zoukankan      html  css  js  c++  java
  • 使用.net备份和还原数据库

    CSDN网友的提问http://community.csdn.net/Expert/TopicView3.asp?id=4929678

    C#实现SQLSERVER2000数据库备份还原的两种方法
    : 方法一(不使用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), 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), 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;
  • 相关阅读:
    solrCloud设置Tomcat jvm内存解决内存溢出的问题
    JAVA 将图片转换为Base64编码
    希望这是一个新的开始,也是一个好的开端
    最全华为鸿蒙 HarmonyOS 开发资料汇总
    iPhone X适配方案
    前端程序员经常忽视的一个JavaScript面试题
    vs for Mac 升级后编译原项目提示找不到“.NETFramework,Version=v5.0”的引用程序集
    使用FastReport的BarCode2D控件生成含中文的PDF417条形码
    vs for Mac中的启用Entity Framework Core .NET命令行工具
    64位Windows7升级IE11后无法启动的解决办法
  • 原文地址:https://www.cnblogs.com/stswordman/p/469002.html
Copyright © 2011-2022 走看看