【问题标题】:How to save .bak file in specific folder using C# (windows application)如何使用 C#(Windows 应用程序)将 .bak 文件保存在特定文件夹中
【发布时间】:2013-07-16 06:48:09
【问题描述】:

在我的程序中,我使用 C# 保存 Sql 备份文件, 在这个程序中,当我点击按钮“SAVEDIALOG”打开时, 但我想将此文件保存在特定文件夹或特定路径中。

意思是,我不想让用户将此文件保存在除特定路径之外的任何地方。

请帮助我, 下面是编码,我在哪个点击事件上保存我的文件。 注:其桌面应用程序使用 C#,SQL server 2008。

private void btnCreate_Click(object sender, EventArgs e)
{
// If there was a SQL connection created

if (srvSql != null)
{

// If the user has chosen a path where to save the backup file

if (saveBackupDialog.ShowDialog() == DialogResult.OK)
{

// Create a new backup operation

Backup bkpDatabase = new Backup();

// Set the backup type to a database backup

bkpDatabase.Action = BackupActionType.Database;
// Set the database that we want to perform a backup on

bkpDatabase.Database = cmbDatabase.SelectedItem.ToString();

// Set the backup device to a file

BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName, DeviceType.File);

// Add the backup device to the backup

bkpDatabase.Devices.Add(bkpDevice);

// Perform the backup

bkpDatabase.SqlBackup(srvSql);

}
}
else
{

// There was no connection established; probably the Connect button was not clicked

MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}
}

【问题讨论】:

  • 如果您只希望他们保存在特定路径中,为什么要向他们显示选择路径的对话框?
  • 是的,正如@Martin Smith 所说,只需向用户询问文件名,如果您不想选择路径,则无需保存对话框

标签: c# sql-server-2008


【解决方案1】:

您可以将路径写入您想要的位置并直接保存而无需询问。您需要删除保存文件对话框。

只需创建一个这样的字符串

string myPath = " a path whic you want to save  ";   


//string myPath = @"C:/backups/mybackup.sql";   

你可以看到上面的固定代码


   private void btnCreate_Click(object sender, EventArgs e)
    {
    // If there was a SQL connection created

    if (srvSql != null)
    {

    // If the user has chosen a path where to save the backup file




    // Create a new backup operation

    Backup bkpDatabase = new Backup();

// Set the backup type to a database backup

bkpDatabase.Action = BackupActionType.Database;
// Set the database that we want to perform a backup on

bkpDatabase.Database = cmbDatabase.SelectedItem.ToString();

// Set the backup device to a file

string myPath = " a path whic you want to save  ";   


//string myPath = @"C:/backups/mybackup.sql";   


BackupDeviceItem bkpDevice = new BackupDeviceItem(myPath , DeviceType.File);

// Add the backup device to the backup

bkpDatabase.Devices.Add(bkpDevice);

// Perform the backup

bkpDatabase.SqlBackup(srvSql);


}
else
{

// There was no connection established; probably the Connect button was not clicked

MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}
 }

【讨论】:

    【解决方案2】:

    使用此代码,这里 DatabaseBackupFilePath 将是保存备份文件的完整 .bak 文件路径。

     using System;
        using System.Configuration;
        using System.IO;
        using System.Data.SqlClient;
        using Microsoft.SqlServer.Management.Smo;
        using Microsoft.SqlServer.Management.Common;
    
    public bool BackupDataBase()
            {
                bool isDatabackedUp = true;
                try
                {
                    Backup sqlBackup = new Backup();
    
                    if (null != sqlBackup)
                    {
                        //Set the Database backup type
                        sqlBackup.Action = BackupActionType.Database;
    
                        //Set database backup description to be kept in the back up log table
                        sqlBackup.BackupSetDescription = Constants.DatabaseBackupDescription +
                                                         DateTime.Now.ToShortDateString();
    
                        sqlBackup.BackupSetName = Constants.DatabaseBackupSetName;
    
                        //Set the backup file path and backup type
                        BackupDeviceItem deviceItem = new BackupDeviceItem(DatabaseBackupFilePath, DeviceType.File);
                        ServerConnection connection = new ServerConnection(this.BackupConnection);
    
                        //Set the database name to backup
                        sqlBackup.Database = LocalDatabaseName;
                        sqlBackup.Initialize = true;
                        sqlBackup.Checksum = true;
                        sqlBackup.ContinueAfterError = true;
    
                        //Set the file details to save the backup
                        sqlBackup.Devices.Add(deviceItem);
                        sqlBackup.Incremental = false;
    
                        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
                        sqlBackup.FormatMedia = false;
    
                        //Intiate the database backup
                        Server sqlServer = new Server(connection);
                        sqlBackup.SqlBackup(sqlServer);
                    }
    
                    return isDatabackedUp;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多