SqlBulkCopy保持失败,出现1129500条记录

时间:2018-10-11 12:20:48

标签: c# sql-server sqlbulkcopy

我很茫然。我有一个表,其中有5年的数据,每年大约有400万条记录,因此该表总共有2000万条记录。我编写了这个C#应用程序,它将选择年份,然后选择季度,然后将数据移动到Archive表中。我尝试使用BatchSize和BulkCopyTimeout,但始终在1129500 or 2093,000记录处超时。

  

有没有更好的方法可以做到这一点,或者我在   代码?

Copied 1129500 so far...
Exception = Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
    public static bool SqlBulkCopy()
    {
        string connectionString = ConfigurationManager.AppSettings.Get("EcommerceConnectionString");
        string BKConnectionString = ConfigurationManager.AppSettings.Get("BKConnectionString");

        // Open a sourceConnection to the AdventureWorks database.
        using (SqlConnection sourceConnection = new SqlConnection(BKConnectionString))
        {
            sourceConnection.Open();

            // Perform an initial count on the destination table.
            string queryString = "SELECT * from GuidelineLog";
            string queryClause = string.Format("where DATEPART(YEAR,LogDate) = '{0}' and DATEPART(QUARTER,LogDate) = '{1}'", 2015, 3);
            string TSQL = string.Format("{0} {1}", queryString, queryClause);
            SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM " + "dbo.GuidelineLogArchive", sourceConnection);
            commandRowCount.CommandTimeout = 900;
            long countStart = System.Convert.ToInt32(
            commandRowCount.ExecuteScalar());

            Console.WriteLine("Starting row count = {0}", countStart);
            WriteLog("Log_10_11_18.txt", String.Format("Starting row count = {0}", countStart));
            // Get data from the source table as a SqlDataReader.
            Console.WriteLine("Source table = {0}", TSQL);
            SqlCommand commandSourceData = new SqlCommand(TSQL, sourceConnection);
            commandSourceData.CommandTimeout = 900;
            SqlDataReader reader = commandSourceData.ExecuteReader();

            // Create the SqlBulkCopy object using a connection string. 
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
            {
                bulkCopy.DestinationTableName = "dbo.GuidelineLogArchive";
                // How many Rows you want to insert at a time
                //bulkCopy.BatchSize = 100000;
                bulkCopy.BatchSize = 500;
                // Set the timeout.
                bulkCopy.BulkCopyTimeout = 0;

                // Set up the event handler to notify after 4500 rows.
                bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
                bulkCopy.NotifyAfter = 4500;

                //(                  2093,000 row(s) affected)
                //Always stopping at 2093,000
                try
                {
                    // Write from the source to the destination.
                    bulkCopy.WriteToServer(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    WriteLog("Log_10_11_18.txt", String.Format("Exception = {0}", ex.Message));
                    return false;
                }
                finally
                {
                    // Close the SqlDataReader. The SqlBulkCopy
                    // object is automatically closed at the end
                    // of the using block.
                    reader.Close();

                }
                return true;
            }

            // Perform a final count on the destination 
            // table to see how many rows were added.
            long countEnd = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
            Console.WriteLine("Ending row count = {0}", countEnd);
            WriteLog("Log_10_11_18.txt", String.Format("Ending row count = {0}", countEnd));
            Console.WriteLine("{0} rows were added.", countEnd - countStart);
            WriteLog("Log_10_11_18.txt", String.Format("{0} rows were added.", countEnd - countStart));
        }
    }

1 个答案:

答案 0 :(得分:1)

我不得不增加阅读器的超时时间900 MS不足以执行和传输

有关enter image description here的注释,请参见

  

此属性是累积超时(对于所有   在调用方法期间被读取)用于所有网络读取   在命令执行或结果处理期间。 可以超时   返回第一行后仍然发生,并且其中不包括用户   处理时间,只有网络读取时间。