简体   繁体   English

在本地计算机上设置数据库备份文件

[英]Set database backup file on local machine

POSSIBLE DUPLICATE OF : 可能的重复:

How to create SQL Server 2008 database full backup programmatically in desired folder 如何在所需的文件夹中以编程方式创建SQL Server 2008数据库完全备份

I have a database on my computer (SQL Server 2008 Express). 我的计算机上有一个数据库(SQL Server 2008 Express)。

I need any sample code in C# that I can use to backup the database to a file using Visual Studio 2010. 我需要C#中的任何示例代码,我可以使用Visual Studio 2010将数据库备份到文件。

Thanks.. 谢谢..

i'll using this code to connect Database 我将使用此代码连接数据库

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;
    }

Just execute SQL Server command 只需执行SQL Server命令

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

from your program 来自你的程序

EDIT 编辑

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;
  }  

Try this peice of code. 试试这个代码。

Im using flowing code to connect database 我使用流动代码连接数据库

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;
}

can i use your SQL Command to set backup using upon type connection 我可以使用您的SQL命令在类型连接上设置备份

This what I did and it Worked! 这就是我所做的和它的工作!

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从本地计算机备份远程MySQL数据库 - Backup remote MySQL database from local machine 远程数据库的 C# SMO 备份到本地机器 - C# SMO backup of remote database to local machine Azure 数据库备份到本地存储 - Azure Database backup to Local Storage 通过C#代码从备份文件还原远程计算机上的MSSQL数据库 - Restore MSSQL database on remote machine from backup file via C# code 创建附加数据库文件的备份 - Create Backup of attached database file 无法将数据库复制为备份文件 - failed to copy database as backup file 在 .net MVC 中是否有任何方法可以将数据库日期导出到不在本地机器中的文件中,并使用 email 作为文件附件发送。 - Is there any method In .net MVC to export database date in file not in local machine and send as file attachment with email.? 部署在本地SQL Server Management Studio中的Azure数据库的数据库备份 - Database backup of azure database deployed in local sql server management studio 提供指向pdf文件的下载链接,以获取SQL Server数据库中存储的路径和本地计算机中存储的文件 - Provide download link to pdf files for the path stored in SQL Server database and file stored in local machine 在本地计算机和数据库服务器计算机之间同步数据 - synchronize data between local machine and database server machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM