1

可能的重复:

如何在所需文件夹中以编程方式创建 SQL Server 2008 数据库完整备份

我的计算机上有一个数据库(SQL Server 2008 Express)。

我需要 C# 中的任何示例代码,可用于使用 Visual Studio 2010 将数据库备份到文件中。

谢谢..

我将使用此代码连接数据库

public SqlConnection SqlSaverConn()
    {
        string path = Application.StartupPath + "\\";
        String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ path +"SMS_DB.mdf;Integrated Security=True;User Instance=True";
        SqlConnection con = new SqlConnection(conStr);
        try
        {
            con.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        return con;
    }
4

3 回答 3

4

只需执行 SQL Server 命令

BACKUP DATABASE database_name TO DISK='d:\path\to\backup\file\on\the\server.bak' 

从你的程序

编辑

public SqlConnection SqlSaverConn()      
{
      string path = Application.StartupPath + "\\";
      String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ path +"SMS_DB.mdf;
                        Integrated Security=True; User Instance=True";
      SqlConnection con = new SqlConnection(conStr);
      try
      {
          con.Open();
          SqlCommand command;
          command = new SqlCommand(@"backup database SMS_DB.mdf to disk ='" + path + "\\" + name, con);
          command.ExecuteNonQuery(); 
          MessageBox.Show("Backup Created."); 
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.ToString());
      }
      return con;
  }  

试试这段代码。

于 2012-07-11T08:53:48.817 回答
0

我使用流动代码连接数据库

public SqlConnection SqlSaverConn()
{
    string path = Application.StartupPath + "\\";
    String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ path +"SMS_DB.mdf;Integrated Security=True;User Instance=True";
    SqlConnection con = new SqlConnection(conStr);
    try
    {
        con.Open();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    return con;
}

我可以使用您的 SQL 命令在类型连接时设置备份吗

于 2012-07-11T13:34:12.690 回答
0

这就是我所做的,它奏效了!

private void BackupButtonClick(object sender, RoutedEventArgs e)
{
    // FILE NAME WITH DATE DISTICNTION
    string fileName = string.Format("SchoolBackup_{0}.bak", DateTime.Now.ToString("yyyy_MM_dd_h_mm_tt"));
    try
    {
        // YOUR SEREVER OR MACHINE NAME
        Server dbServer = new Server (new ServerConnection("DESKTOP"));
        Microsoft.SqlServer.Management.Smo.Backup dbBackup = new Microsoft.SqlServer.Management.Smo.Backup()
        {
            Action = BackupActionType.Database, 
            Database = "School"
        };

        dbBackup.Devices.AddDevice(@backupDirectory() +"\\"+ fileName, DeviceType.File);
        dbBackup.Initialize = true;
        dbBackup.SqlBackupAsync(dbServer);


        MessageBox.Show("Backup", "Backup Completed!");
    }
    catch(Exception err)
    {
        System.Windows.MessageBox.Show(err.ToString());
    }
}


// THE DIRECTOTRY YOU WANT TO SAVE IN
public string backupDirectory()
{
    using (var dialog = new FolderBrowserDialog())
    {
        var result = dialog.ShowDialog();
        return dialog.SelectedPath;
    }
}
于 2018-09-05T11:14:21.817 回答