0

私のプログラムでは、C#を使用してSqlバックアップファイルを保存しています。このプログラムでは、ボタン「SAVEDIALOG」をクリックすると開きますが、このファイルを特定のフォルダーまたは特定のパスに保存したいです。

つまり、ユーザーがこのファイルを特定のパス以外の場所に保存できるようにしたくありません。

親切に助けてください。以下はコーディングで、クリックイベントでファイルを保存しています。注記: C#、SQL サーバー 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);

}
}
4

2 に答える 2

0

必要な場所にパスを書き込んで、尋ねることなく直接保存できます。savefile ダイアログを削除する必要があります。

このような文字列を作成するだけです

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

}
 }
于 2013-07-16T07:03:20.853 に答える
0

このコードを使用します。ここで、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;
            }
        }
于 2013-07-16T07:03:42.423 に答える