为什么smo备份在给定直接路径时有效,而不是通过保存文件对话框

时间:2014-09-22 08:04:09

标签: c# smo savefiledialog

您好我在尝试设置打开保存文件对话框的程序,然后使用该保存位置使用smo备份远程SQL Server。当我直接给它网络位置时,它工作。

 if (checkBox7.Checked)
        {

                // Displays a SaveFileDialog so the user can save the Backup
                // assigned to Button2.
                try
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.Title = "Save SQL Backup";
                    saveFileDialog1.CheckFileExists = false;
                    saveFileDialog1.CheckPathExists = true;
                    saveFileDialog1.DefaultExt = "bak";
                    saveFileDialog1.Filter = "SQL Backup (*.bak)|*.bak";
                    saveFileDialog1.FilterIndex = 1;
                    saveFileDialog1.InitialDirectory = "\\\\" + Server_Name;
                    saveFileDialog1.RestoreDirectory = false;

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        SaveFileName = saveFileDialog1.FileName;
                    }
                    saveFileDialog1.Dispose();
                }
                catch (Exception exsavefile)
                {
                    stfrm.txtMessages.AppendText(String.Format("An Error has occured in the save dialog process \n\nMessage: {0}", exsavefile.Message) + Environment.NewLine);
                }
            try
            {
                //FileNameBackup = "\\\\bmslondon01\\SQL Backups\\Test\\test folder\\test.bak";
                stfrm.txtMessages.AppendText(String.Format("File Location Chosen: {0}", FileNameBackup) + Environment.NewLine);

                Backup bkp = new Backup();
                bkp.Action = BackupActionType.Database;
                bkp.Database = SelectDatabase_Name;
                bkp.Devices.AddDevice(FileNameBackup, DeviceType.File);
                stfrm.pbCurrent.Style = ProgressBarStyle.Continuous;
                stfrm.pbCurrent.Value = 0;
                stfrm.pbCurrent.Maximum = 100;
                stfrm.pbCurrent.Value = 10;

                bkp.PercentCompleteNotification = 10;
                bkp.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);

                connbackup = new ServerConnection();
                connbackup.ServerInstance = Server_Name;

                srv = new Server(connbackup);
                bkp.SqlBackup(srv);

                stfrm.txtMessages.AppendText(String.Format("Database Backup to {0}", SaveFileName) + Environment.NewLine);
                stfrm.pbOverall.Value = 15;
                stfrm.pbCurrent.Style = ProgressBarStyle.Marquee;
            }
            catch (Exception exBackup)
            {
                stfrm.txtMessages.AppendText(String.Format("An Error has occured -  Sql Error Message: {0} \n\n  - File failed: {1}", exBackup.Message, SaveFileName) + Environment.NewLine);
                stfrm.txtMessages.ScrollToCaret();
                stfrm.pbOverall.Value = 15;
            }

正如你所看到的那样,有一个注释,就像我直接设置FileNameBackup并且它工作但是当我设置它时认为保存文件对话框它不起作用。有没有人有任何建议?

0 个答案:

没有答案