0

我已经完成BulkCopy of List using FastMember and Threading了更快的性能。

实际上,我需要像999999999记录一样批量复制,并且我为每条1000000记录设置了一个间隔并Clearing the List避免OutofMemory Exception

整个过程16 Hours完成了,现在,我的问题是:No records have been inserted。在进行过程中,我已经使用读取数据(nolock)并且我的查询正在显示数据,有时它正在显示No Records。我认为数据一直在插入和删除.

原因,我说的是:如果我现在开始插入任何记录,身份列将显示非常大的数以百万计。
所以,据我所知,我觉得记录已经被插入和删除。

我的代码:

private void Btn_Start_Encrypting_Click(object sender, EventArgs e)
        {
            try
            {
                Btn_Start_Encrypting.Enabled = false;

                Lbl_Msg1.Text = "Time Started : " + DateTime.Now.ToString();

                DateTime StartTime = DateTime.Now;                

                #region List<CustomClass> with Random Numbers

                Random rand = new Random();

                Thread previousThread = null;

                for (int i = 1; i <= 999999999; i++)
                {
                    LstEncData.Add(new EncryptedData { MESSAGE_ID = rand.Next(1, 100), ENCRYPTED_DATA = EncryptDecrypt.Encrypt(i.ToString("D9")) });

                    if (i % 1000000 == 0)
                    {
                        LstEncData = LstEncData.OrderBy(p => p.MESSAGE_ID).ToList();

                        if (previousThread != null)
                        {
                            previousThread.Join();
                        }

                        List<EncryptedData> copyList = LstEncData.ToList();

                        previousThread = new Thread(() => BulkCopyList(copyList));
                        previousThread.Start();

                        LstEncData.Clear();
                    }
                }

                if (LstEncData.Count > 0)
                {
                    if (previousThread != null)
                    {
                        previousThread.Join();
                    }

                    List<EncryptedData> copyList = LstEncData.ToList();

                    previousThread = new Thread(() => BulkCopyList(copyList));
                    previousThread.Start();

                    LstEncData.Clear();
                }

                previousThread.Join();

                Lbl_Msg2.Text = "Time Ended : " + DateTime.Now.ToString();

                DateTime EndTime = DateTime.Now;

                TimeSpan duration = EndTime - StartTime;

                Lbl_Msg3.Text = "Total Time Taken in Seconds : " + duration.TotalSeconds.ToString();

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Btn_Start_Encrypting.Enabled = true;
            }
        }

public void BulkCopyList(List<EncryptedData> Lst)
        {
            try
            {
                using (var bcp = new SqlBulkCopy(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]))
                {
                    using (var reader = ObjectReader.Create(Lst, "ENCRYPTED_DATA"))
                    {
                        SqlBulkCopyColumnMapping mapEncryData = new SqlBulkCopyColumnMapping("ENCRYPTED_DATA", "ENCRYPTED_DATA");
                        bcp.ColumnMappings.Add(mapEncryData);

                        bcp.DestinationTableName = "EncryptedData1";
                        bcp.WriteToServer(reader);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

我在 Threading (或) BulkCopy 附近做错了吗?

4

0 回答 0