SQL Server中SMO備份資料庫進度條不顯示?

來源:互聯網
上載者:User

邢少提到一個奇怪的問題,用SMO備份資料庫時不顯示進度條,也就是進度條事件PercentComplete不觸發。

今天試了一下,果然有點奇怪。

代碼如下:

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
private void btnBackup_Click(object sender, EventArgs e)
        {
            btnBackup.Enabled = false;

            Thread tr = new Thread(new ThreadStart(doBackup));
            tr.Priority = ThreadPriority.AboveNormal;
            tr.Start();
            //Thread.Sleep(3000);
        }
        /// <summary>
        /// 備份資料庫
        /// </summary>
        public void doBackup()
        {

            pbDemo.Value = 0;
            pbDemo.Maximum = 100;
            pbDemo.Style = ProgressBarStyle.Blocks;
            //pbDemo.Step = 10;

            Server srv = new Server(@"(local)");
            Backup backup = new Backup();

            backup.Action = BackupActionType.Database;
            backup.Database = "btnet";
            backup.Incremental = false;

            backup.Devices.Add(new BackupDeviceItem(@"C:\agronet09.bak", DeviceType.File));
            backup.Initialize = true;
            backup.PercentCompleteNotification = 10;
            backup.PercentComplete += new PercentCompleteEventHandler(backup_PercentComplete);

            //backup.Checksum = true;

            backup.SqlBackup(srv);
        }

        public void backup_PercentComplete(object sender, Microsoft.SqlServer.Management.Smo.PercentCompleteEventArgs e)
        {
            this.Invoke(new displayProgress_delegate(displayProgress), e.Percent);
            //Application.DoEvents();
        }

        public delegate void displayProgress_delegate(int progress);

        public void displayProgress(int progress)
        {
            this.lbProgress.Text = "已完成[" + progress.ToString() + " %]";
            pbDemo.Value = progress;
            btnBackup.Enabled = (progress == 100);

        }

癥狀如下:結果正確執行,但進度條不顯示。

剛開始以為是線程問題,後來發現不是這個原因。

又試了另外一段代碼

 using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Diagnostics;

  static void Main(string[] args)
        {
            BackupDatabase("ap4\\agronet09", "agronet2008", "c:\\Northind_3.bak");
            Console.WriteLine(Environment.NewLine + "Press any key to continue.");
            Console.ReadKey();
        }
        public static  void BackupDatabase(string serverName, string databaseName, string fileName)
        {
            Console.WriteLine("*** Backing up ***");
            Server server = new Server(serverName);
            Backup backup = new Backup();
            backup.Action = BackupActionType.Database;
            backup.Database = databaseName;
            backup.Incremental = false;
            backup.Initialize = true;
            backup.LogTruncation = BackupTruncateLogType.Truncate;
            BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
            backup.Devices.Add(backupItemDevice);
            backup.PercentCompleteNotification = 10;
            backup.PercentComplete += backup_PercentComplete;
            backup.Complete +=backup_Complete;
            backup.SqlBackup(server);
        }
        protected static  void backup_PercentComplete(object sender, PercentCompleteEventArgs e)
        {
            Console.WriteLine(e.Percent + "% processed.");
            //Application.();
            System.Threading.Thread.Sleep(1000);
        }
        protected static  void backup_Complete(object sender, ServerMessageEventArgs e)
        {
            Console.WriteLine(Environment.NewLine + e.ToString());
        }

結果還是不顯示。

後來突然想到會不會是檔案太小,試了一個200M的資料檔案,果然成功顯示:

後來經反覆實驗,發現SQL server 2000約在資料檔案加記錄檔大於6M左右時顯示進度條。

而Sql server 2008 r2大約在3M時顯示進度條。可能跟機器也有關係。

 注意:SQL server 2008 r2版規定主要資料檔案必須大於3M,微軟真牛!

參考文章:

http://msdn.microsoft.com/zh-cn/magazine/cc163409.aspx

示範代碼下載

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.