執行方法Winforms后更新TextBox

[英]Updating TextBox After Execution of a Method Winforms


I'm making a C# app to resotre many .bak Files stored in a directory. It works fine now, but I have one problem I can't seem to resolve and would like your help. The ExecuteNonQuery Takes a While to restore one .bak file and i want to update a textbox with a message everytime the ExecuteNonQuery completes.

我正在制作一個C#應用程序來重新安排存儲在目錄中的許多.bak文件。它現在工作正常,但我有一個問題似乎無法解決,並希望得到你的幫助。 ExecuteNonQuery需要一段時間才能恢復一個.bak文件,我希望每次ExecuteNonQuery完成時都用消息更新文本框。

I tried to call the ExecuteNonQueryAsync but i get this Message :

我試圖調用ExecuteNonQueryAsync,但我收到此消息:

The connection does not take in charge MultipleActiveResultSets

該連接不會收取MultipleActiveResultSets

My question is: How can I update The TextBox each time the executeNonQuery finishes?

我的問題是:每次executeNonQuery完成后如何更新TextBox?

Here is my code, please advise :

這是我的代碼,請指教:

foreach (FileInfo file in Files)
        {
            string DbNamequery = "RESTORE FILELISTONLY FROM DISK = N'" + FolderBrowser.SelectedPath + @"\" + file.Name + "'" ;

            sqlform.ConSQL.PrepareQuery(DbNamequery);

            try
            {
              string DbName = sqlform.ConSQL.GetDataBaseName();
              string RestoreQuery = "RESTORE DATABASE [" + file.Name + "] FILE = N'" + DbName + "' FROM  DISK = N'" + FolderBrowser.SelectedPath + @"\" + file.Name;
                     RestoreQuery += "' WITH  FILE = 1,MOVE N'" + DbName + @"' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\" + file.Name + ".mdf'";
                     RestoreQuery += ",MOVE N'" + DbName + @"_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\" + file.Name + "_0.ldf',";
                     RestoreQuery += "NOUNLOAD,  STATS = 10";

                sqlform.ConSQL.PrepareQuery(RestoreQuery);

                sqlform.ConSQL.ExecuteQuery();

                ReportTextBox.Text += "Data Base : " + DbName + " Has been Restored With this Name :" + file.Name + " Successfully \r\n";

            }
            catch (Exception ex)
            {
                ReportTextBox.Text += "Couldn't Resotre File " + file.Name + " Because of The following : \r\n" + ex.Message;
            }
        }

Definition for ExecuteQuery() :

ExecuteQuery()的定義:

  public async void ExecuteQuery()
    {
      int x = await  cmd.ExecuteNonQueryAsync();   
    }

When I execute The Query synchronously it works fine but the ReportTextbox doesn't get updated until the loop has ended.

當我同步執行查詢時它工作正常但ReportTextbox在循環結束之前不會更新。

Also this code is wrapped in a Click Button Event

此代碼也包含在Click Button事件中

Thanks in advance

提前致謝

1 个解决方案

#1


0  

First of all you shouldn't have async void method rather return a Task

首先,你不應該使用異步void方法而是返回一個Task

  public async Task<int> ExecuteQuery()
    {
      int x = await cmd.ExecuteNonQueryAsync(); 
      return x;  
    }

In your calling code, await the method call and update textbox, hopefully you want to update the textbox text only if the RESTORE operation succeeds

在您的調用代碼中,等待方法調用並更新文本框,希望您只有在RESTORE操作成功時才​​更新文本框文本

int result = await sqlform.ConSQL.ExecuteQuery();
if(result > 0) 
ReportTextBox.Text += "Data Base : " + DbName + " Has been Restored With this Name :" + file.Name + " Successfully \r\n";

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/08/08/725fde3ad55458ff3db7516be558a770.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com