2016年9月3日 星期六

使用非同步的方式執行SQL命令

連線字串加上Asynchronous Processing=True啟動非同步模式

比如:
            String strConnectionString = "Data Source=localhost;" +
                                         "Integrated Security=True;" +
                                         "Initial Catalog=AdventureWorks;" +
                                         "Asynchronous Processing=True";

用途:同步模式是單執行緒,如果查詢結果要等待很久,程式會發生無法回應的狀態,就必須改用非同步

備註:
IAsyncResult result = cmd.BeginExecuteReader();

                    while (!result.IsCompleted)
                    {
                        System.Threading.Thread.Sleep(100);
                        textBox1.Text = "執行中!";
                    }


REF:
Visual C# 2012資料庫程式設計暨進銷存系統實作
第5-43頁
程式5-8-1

沒有留言:

張貼留言