简体   繁体   中英

How to save .bak file in specific folder using C# (windows application)

In my program I am saving Sql backup file using C#, In this program when I click on Button "SAVEDIALOG" open, But I want to save this file in specific folder or specific path.

Means, i dont want to allow user to save this file in anywhere except specific path.

Kindly Help me for it, Below is coding, on which click event I am saving My file. Noted: Its Desktop application using 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);

}
}

You can write your patth where you want and save it directly without asking. You need to delete savefile dialog.

Just create a string like this

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


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

you can see fixed code above


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

}
 }

Use this code, Here DatabaseBackupFilePath will be the full .bak file path where the back up file will be saved.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM