如何将备份或还原命令发送到命令窗口

时间:2017-02-20 21:52:07

标签: c# sql-server-2008 command-line-interface

好的,我想我需要澄清一些我之前忘记提及的帖子。我的测试应用程序"在Visual Studio 2010中有一个SQL Server 2008 R2 EXPRESS 数据库。但是,数据库是独立的SQL Server Express安装中的 Not 。相反,数据文件,即.mdf和.ldf来自VS" Project \ Add New Item \ Data \ Service-based Database"中的选择。因此,我的" BkUp_SMO.mdf"数据文件。

我不确定上述内容是否有所不同,但我尝试了多个使用Microsoft.SqlServer.Management对象SMO的示例,但没有成功。我添加了所需的.DLL,即Microsoft.SqlServer.ConnectionInfo,Microsoft.SqlServer.Management.Sdk.Sfc,Microsoft.SqlServer.Smo,Microsoft.SqlServer.SmoExtended。

在我的代码中,我有"使用" Microsoft.SqlServer.Management.Common和Microsoft.SqlServer.Management.Smo的语句。 我甚至添加了一个"使用" for System.Deployment.Application,以便使用String dbPath = ApplicationDeployment.CurrentDeployment.DataDirectory;

将路径的字符串值设置回DB和日志文件所在的ClickOnce部署文件夹。

除了下面引用的文章之外,我还尝试了另一篇文章中的示例,即"在C#中备份SQL数据库" Backing up an SQL Database in C#

是否无法在Visual Studio创建的SQL数据库上执行备份和还原?

我在C#中编写了测试应用程序,目的是通过命令行发送SQL Server备份或还原命令。我在一篇标题为“从命令行备份和还原SQL Server数据库”的文章中提供了一些代码

Backup and Restore Your SQL Server Database from the Command Line

完整的应用程序将是user_App,我不希望最终用户必须打开命令窗口并键入任何内容,因此我尝试通过C#代码发送所需的命令如下所示。我的问题是,代码运行没有异常,CMD窗口打开和关闭,但没有发生我的SQL Server 2008 R2数据文件(.mdf)的任何备份。

请建议我在代码中遗漏的内容,或更好的方法来完成此操作。 此外,完全备份是否会自动备份日志文件(.ldf)?

First code attempt
private void btnChoose_Click(object sender, EventArgs e)
{
    if (optBkupCMD.Checked)
    {
        StringBuilder bkup = new StringBuilder();
        bkup.Append("SqlCmd -E -S ");
        bkup.Append(Environment.MachineName);//servername appears to be same as computer name.
        bkup.Append(" –Q “BACKUP DATABASE [BkUp_SMO.mdf] TO DISK=’C:\\Backups\\BkUp_SMO.bak'”");
        string theBackup = bkup.ToString();

    using (Process process = new Process())
    {
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = false;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.Arguments = @"/C";
        process.Start();
        process.StandardInput.WriteLine(theBackup);
        process.StandardInput.Flush();
        process.StandardInput.Close();
        process.WaitForExit();
        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }
else if (optRestoreCMD.Checked)
{
    StringBuilder rstr = new StringBuilder();
    rstr.Append("SqlCmd -E -S ");
    rstr.Append(Environment.MachineName);
    rstr.Append(" –Q “RESTORE DATABASE [BkUp_SMO.mdf] FROM DISK=’C:\\Backups\\BkUp_SMO.bak'”");
    string restore = rstr.ToString();

    using (Process process = new Process())
    {
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = false;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.Arguments = @"/C";
        process.Start();
        process.StandardInput.WriteLine(restore);
        process.StandardInput.Flush();
        process.StandardInput.Close();
        process.WaitForExit();
        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }
}

}

My 2nd code attempt.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Deployment.Application;
using System.Diagnostics;
using System.IO;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace DB_Bkup_using_SMO
{
    public partial class Bkup_in_CSharp : Form
    {
        public Bkup_in_CSharp()
        {
            InitializeComponent();
        }

    private void btnBkViaCsharp_Click(object sender, EventArgs e)
    {
        string filePath = ApplicationDeployment.CurrentDeployment.DataDirectory;
        BackupDatabase(filePath);
    }

    private void btnRestViaCsharp_Click(object sender, EventArgs e)
    {
        string filePath = ApplicationDeployment.CurrentDeployment.DataDirectory;
        RestoreDatabase(filePath);
    }

    ///<summary>
    ///Backup a whole database to the specified file.
    ///</summary>
    ///<remarks>
    ///The database must not be in use when backing up.
    ///The folder holding the file must have appropriate permissions given
    ///</remarks>
    ///<param name="backupFile">Full path to file to hold the backup</param>
    public static void BackupDatabase(string backupFile)
    {
        try
        {
            ServerConnection con = new ServerConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BkUp_SMO.mdf;Integrated Security=True;User Instance=True");

            Server server = new Server(con);
            Backup source = new Backup();
            source.Database = "BkUp_SMO.mdf";
            source.Action = BackupActionType.Database;

            source.LogTruncation = BackupTruncateLogType.Truncate;
            BackupDeviceItem destination = new BackupDeviceItem(backupFile, DeviceType.File);
            source.Devices.Add(destination);

            source.SqlBackup(server);
            con.Disconnect();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " " + ex.InnerException);
        }
    }

    ///<summary>
    ///Restore a whole database from a backup file.
    ///</summary>
    ///<remarks>
    ///The database must be in use when backing up.
    ///The folder holding the file must have appropriate permissions given.
    ///</remarks>
    ///<param name="backupFile">Full path to file to holding the backup</param>
    public static void RestoreDatabase(string backupFile)
    {
        try
        {
            ServerConnection con = new ServerConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BkUp_SMO.mdf;Integrated Security=True;User Instance=True");

            Server server = new Server(con);
            Restore destination = new Restore();
            destination.Database = "BkUp_SMO.mdf";
            destination.Action = RestoreActionType.Database;
            destination.Action = RestoreActionType.Log;
            BackupDeviceItem source = new BackupDeviceItem(backupFile, DeviceType.File);
            destination.Devices.Add(source);
            destination.ReplaceDatabase = true;
            destination.SqlRestore(server);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " " + ex.InnerException);
        }

    }
}
}

1 个答案:

答案 0 :(得分:0)

这是我的备份SQL Server 2008 R2的代码。

如果您尝试搜索,这种基本示例代码很多。

尝试但不需要将此标记为答案,因为这里有很多。

string masterdb_ConnectionString = string.Format(@"Data Source={0};Initial Catalog=Master;Connect Timeout=79;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Integrated Security=True;", System.Environment.MachineName);

using (SqlConnection masterdbConn = new SqlConnection())
{
    masterdbConn.ConnectionString = mastedb_rConnectionString;
    masterdbConn.Open();

    using (SqlCommand multiuser_rollback_dbcomm = new SqlCommand())
    {
        multiuser_rollback_dbcomm.Connection = masterdbConn;
        multiuser_rollback_dbcomm.CommandText= @"ALTER DATABASE yourdbname SET MULTI_USER WITH ROLLBACK IMMEDIATE";
        multiuser_rollback_dbcomm.CommandTimeout = 79;

        multiuser_rollback_dbcomm.ExecuteNonQuery();
    }
    masterdbConn.Close();
}

SqlConnection.ClearAllPools();

string yourdb_ConnectionString= "connectionstring for yourdb here";

using (SqlConnection backupConn = new SqlConnection())
{
    backupConn.ConnectionString = yourdb_ConnectionString;
    backupConn.Open();

    using (SqlCommand backupcomm = new SqlCommand())
    {
        backupcomm.Connection = backupConn;
        backupcomm.CommandText = string.Format(@"BACKUP DATABASE yourdbname TO DISK='c:\yourdbname.bak'", DateTime.Today.ToString("yyyy/MM/dd"));
        backupcomm.CommandTimeout = 79;

        backupcomm.ExecuteNonQuery();
    }
    backupConn.Close();
}

更新 - 是否会在客户端(用户)的计算机上安装SQL Server 2008 R2?

您已经在使用'Integrated Security = True',这意味着使用此连接字符串的用户将拥有管理员的所有(完整)权限。连接到主数据库显然没有问题。

关于IMMEDIATE ROLLBACK,这将完成所有未完成的事务,就像“在备份之前从数据库中取消”一样。换句话说,在我们关闭一家餐馆之前,如果我们不宣布这家餐厅将要关闭,如果我们突然关闭餐厅,一些顾客可能仍然在餐馆关闭时仍然有食物。

看看ALTER DATABASE IMMEDIATE ROLLBACK, Technet

  

此示例在第一个ALTER DATABASE语句中使用终止选项WITH ROLLBACK IMMEDIATE。将回滚所有未完成的事务,并立即断开与AdventureWorks2008R2示例数据库的任何其他连接。

最后,看来你正在尝试使用SMO。我经历了几天的困难,但终于失败了,走了另一条道路。