Unable to get updated value from method of other class

.net ado.net c# sqlbulkcopy

Question

Below is my class:

MsSql.cs:

public class MSSqlBLL
{
    public static long RowsCopied { get; set; }

    public long BulkCopy()
    {
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
        {
            bulkCopy.DestinationTableName = "dbo.Table1";
            bulkCopy.BatchSize = 100;
            bulkCopy.SqlRowsCopied +=
                new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
            bulkCopy.NotifyAfter = 100;
            try
            {
                bulkCopy.WriteToServer(reader);

            }
            return RowsCopied;
        }
    }

    private static void OnSqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
    {
        RowsCopied = RowsCopied + e.RowsCopied;
    }
}

I am calling BulkCopy function from this class and i want to get currently processed record in my affected records variable.

For eg :For each iteration of loop i would like to get affected records in my affectedRows variable.

public class MySqlBLL
{
    public void GetTotalRows()
        {
            int  totalRecords = 500;
            var table = "Table1";
            for (int i = 0; i < totalRecords / 100; i++) //
             {
                     query = "SELECT * FROM " + table + " LIMIT " + 0 + "," + 100;
                     var reader = Execute(conn, query);
                     long affectedRecords = msSql.BulkCopy();
                     reader.Close();
             }
        }
}

In the above method i am sending chunk by chunk data to BulkCopy method to perform bulk copy but for each bulk copy i would like to get number of records that are processed by bulk copy but the problem is i am getting 0 in affectedRecords variable.

I want to get access current rows processed by sql bulk copy.

1
0
12/21/2016 10:37:57 AM

Popular Answer

The RowsCopied property is only updated after 100 records are copied (as set using NotifyAfter). If you place

Console.WriteLine("Copied {0} so far...", e.RowsCopied);

in OnSqlRowsCopied event handler you will get ongoing progress in case of Console app. But in your case you can simply select count(*) from source table to show the count.

-Source

2
12/21/2016 11:00:06 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow