Dapper批量删除如何报告进度

时间:2019-02-21 16:13:22

标签: dapper

我正在使用Dapper Plus通过另一个表的查询进行批量插入,我的问题是一个报告的进度如何。如您所见,我正在使用后台工作进程运行我的代码,该代码运行正常,但是由于批量删除方法没有reportprogress事件,我将如何处理该事件。

public StockDeativationForm()
{
        InitializeComponent();
        this._backgroundWorker.DoWork += new DoWorkEventHandler(this.BackgroundWorkerDoWork);
        this._backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(this.BackgroundWorkerProgressChanged);
        this._backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.BackgroundWorkerRunWorkerCompleted);

 }

private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
     ProcessStockItems();
}

private void BackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
        this.progressBar2.Value = e.ProgressPercentage;

 }

private void BackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
        this.CompleteProcess();
 }


private void CompleteProcess()
{
        MessageBox.Show("Stock items Deleted", "Stock Item Delete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        base.Close();
 }

public StockDeativationForm()
{
        InitializeComponent();
        this._backgroundWorker.DoWork += new DoWorkEventHandler(this.BackgroundWorkerDoWork);
        this._backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(this.BackgroundWorkerProgressChanged);
        this._backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.BackgroundWorkerRunWorkerCompleted);

}

private void ProcessStockItems()
{
    string conStr = ConfigurationManager.AppSettings["DeleteStock"];
    using (var connection = new SqlConnection(conStr))
    {

      DialogResult _dialogResult = MessageBox.Show(null, "Are you sure you want to delete stock? This will delete all stock items", "Delete Stock", MessageBoxButtons.YesNo);
       if (_dialogResult == DialogResult.Yes)
       {

  connection.BulkDelete(connection.Query<StockItems>("Select ItemID FROM StockItem").ToList());
        }
     }
 }

如何使用bulkdelete方法将报告退回

  connection.BulkDelete(connection.Query<StockItems>("Select ItemID FROM StockItem").ToList());

1 个答案:

答案 0 :(得分:1)

免责声明:我是Dapper Plus

的所有者

您是对的

也许使用log事件可能有用吗?

StringBuilder log = new StringBuilder();

connection.UseBulkOptions(options => options.Log = s => {
    if(s.Contains("...xyz...")) {
        log.AppendLine(s);
    }   
}).BulkDelete(connection.Query<StockItems>("Select ItemID FROM StockItem").ToList());

另一种解决方案是将该请求报告给我们的支持团队,并要求进行“报告进度/通知”事件。