Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlBulkCopy在1129500条记录中不断失败_C#_Sql Server_Sqlbulkcopy - Fatal编程技术网

C# SqlBulkCopy在1129500条记录中不断失败

C# SqlBulkCopy在1129500条记录中不断失败,c#,sql-server,sqlbulkcopy,C#,Sql Server,Sqlbulkcopy,我有点不知所措。我有一个表,有5年的数据,每年大约有400万条记录,所以这个表总共有2000万条记录。我编写了这个C应用程序,它将选择年份,然后选择季度,并将数据移动到归档表中。我尝试过使用BatchSize和BulkCopyTimeout,但它总是在1129500或2093000条记录时超时 有没有更好的方法来做这件事,或者是我在工作中遗漏了什么 密码 我不得不增加读卡器上的超时时间900毫秒不足以执行和传输 有关详细信息,请参见本说明 此属性是所有已删除的网络数据包的累计超时 在调用用于所有

我有点不知所措。我有一个表,有5年的数据,每年大约有400万条记录,所以这个表总共有2000万条记录。我编写了这个C应用程序,它将选择年份,然后选择季度,并将数据移动到归档表中。我尝试过使用BatchSize和BulkCopyTimeout,但它总是在1129500或2093000条记录时超时

有没有更好的方法来做这件事,或者是我在工作中遗漏了什么 密码


我不得不增加读卡器上的超时时间900毫秒不足以执行和传输

有关详细信息,请参见本说明

此属性是所有已删除的网络数据包的累计超时 在调用用于所有网络读取的方法期间读取 在命令执行或结果处理期间。暂停罐 在返回第一行后仍然发生,并且不包括用户 处理时间,仅网络读取时间


源/目标是否在同一台服务器上?您是否尝试过简单地在dest SELECT中执行插入操作。。。从src那里…@AlexK。是的,源/目标在同一台服务器上,我可以执行简单的插入。我不确定它是否能解决超时问题,但您应该始终使用参数化sql,并避免字符串连接,以向sql语句添加值。看,还有。超时需要多长时间?您是否尝试过增加“SqlDataReader”超时?可能是原始查询的超时-900毫秒可能不足以执行和传输所有数据。
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));
        }
    }