Search code examples
dapper-plus

Dapper Bulk Delete how to report progress


I am using Dapper Plus to do a bulk insert using a query from another table my question is here how does one report progress. As you see I am using the background worker process to run my code which works fine however as the bulk delete method does not have a reportprogress event how would I handle this.

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());
        }
     }
 }

How does one report progress back using this method of bulkdelete

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

Solution

  • Disclaimer: I'm the owner of Dapper Plus

    You are right,

    Perhaps using the log event could work?

    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());
    

    Another solution will be to report this request to our support team and ask for a Report Progress/Notify event.