【问题标题】:how to use background worker class for stored procedure execution in c#如何在c#中使用后台工作者类执行存储过程
【发布时间】:2016-02-19 06:59:34
【问题描述】:

我在 C# 中有以下代码,我正在尝试使用后台工作程序类来显示存储过程的执行进度。

或者(非常感谢任何输入如何在通过 c# 执行存储过程时显示进度消息)

问题是当我单击按钮时,代码中没有执行进度,不知道问题发生在哪里。如果有人有什么想法,请告诉我。

谢谢

//Butoon click code
 private void button_executeBL_Click(object sender, EventArgs e)
    {
        if (!backgroundWorker1.IsBusy)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        else
        {
            label1.Text = "Busy Processing, Please wait";
        }

    }

    //background worker class process...

   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(200);
        int count =1;
        string connectionString = "Data Source=.;Initial     Catalog=VegetablesCoSD;Integrated Security=True";
        string commandText = "CoSD.BusinessLogic";

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand(commandText, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 600;
            try
            {
                conn.Open();

                //count will be the number of rows updated. will be zero if no rows updated.
                 backgroundWorker1.ReportProgress( count = cmd.ExecuteNonQuery());

                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    backgroundWorker1.ReportProgress(0);
                    return;
                }

                e.Result = count;
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Update Failed coz.. " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }


    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = e.ProgressPercentage.ToString();

    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            label1.Text = "Process Cancelled";
        }
        else if (e.Error != null)
        {
            label1.Text = e.Error.Message;
        }

        else
        {
            MessageBox.Show("Business Rules Executed Successfully!!!");
            label1.Text = "Total Records Inserted" + e.Result.ToString();
        }

    }

    private void button_Cancel(object sender, EventArgs e)
    {
        if (backgroundWorker1.IsBusy)
        {
            backgroundWorker1.CancelAsync();
        }

        else
        {
            label1.Text = " No Operation in progress to cancel";
        }


    }

【问题讨论】:

  • ExecuteNonQuery() 阻塞,直到存储过程完成,你不会从你的 SP 取回任何进展。也许您可以使用this 让您的 SP 报告进度,以便您可以在 c# 中跟踪它
  • 太棒了...谢谢会试试这个...
  • 经过更多研究后,级别超过 10 的 RAISEERROR 会创建 SqlException,所以这可能不是一个好主意。
  • 是的,它似乎是......有没有其他方法可以通过这个过程?......现在正在执行查询并显示最终结果消息......但进度条是不采取行动....
  • 这似乎是可能的。 Here a full solution with RAISEERROR

标签: c# winforms windows-applications


【解决方案1】:

由于您要运行异步任务,您应该将按钮单击方法更改为 async void,以便您可以等待您的 backgroundWorker1.RunWorkerAsync();

未等待的异步方法将始终同步运行。始终等待异步方法以异步运行它们。

private async void button_executeBL_Click(object sender, EventArgs e)
{
    if (!backgroundWorker1.IsBusy)
    {
        await backgroundWorker1.RunWorkerAsync();
    }

    else
    {
        label1.Text = "Busy Processing, Please wait";
    }

}

【讨论】:

  • 嗨,john 试过这个.. 是的,它成功了,但是正如上面的 cmets 中提到的,当 SP 正在执行时没有显示任何进展......除了'execute'之外,必须有一些方法非查询'获取c#中SP的进度
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2011-10-30
  • 2011-03-17
相关资源
最近更新 更多