bil*_*bob 13 c# sql-server bulkinsert sqldatareader sqlbulkcopy
我们已经有一个运行系统来处理所有连接字符串(db2,oracle,MSServer).
目前,我们正在使用ExecuteNonQuery()一些插入.
我们希望通过使用SqlBulkCopy()而不是改进性能ExecuteNonQuery().我们有一些客户拥有超过5000万条记录.
我们不想使用SSIS,因为我们的系统支持多个数据库.
我创建了一个示例项目来测试其性能SqlBulkCopy().我为MSServer创建了一个简单的读取和插入函数
这是小功能:
public void insertIntoSQLServer()
{
    using (SqlConnection SourceConnection = new SqlConnection(_sourceConnectionString))
    {
        //Open the connection to get the data from the source table
        SourceConnection.Open();
        using (SqlCommand command = new SqlCommand("select * from " + _sourceSchemaName + "." + _sourceTableName + ";", SourceConnection))
        {
            //Read from the source table
            command.CommandTimeout = 2400;
            SqlDataReader reader = command.ExecuteReader();
            using (SqlConnection DestinationConnection = new SqlConnection(_destinationConnectionString))
            {
                DestinationConnection.Open();
                //Clean the destination table
                new SqlCommand("delete from " + _destinationSchemaName + "." + _destinationTableName + ";", DestinationConnection).ExecuteNonQuery();
                using (SqlBulkCopy bc = new SqlBulkCopy(DestinationConnection))
                {
                    bc.DestinationTableName = string.Format("[{0}].[{1}]", _destinationSchemaName, _destinationTableName);
                    bc.NotifyAfter = 10000;
                    //bc.SqlRowsCopied += bc_SqlRowsCopied;
                    bc.WriteToServer(reader);
                }
            }
        }
    }
}
当我在我的dummyTable中少于20万时,批量复制工作正常.但是,当它超过20万条记录时,我有以下错误:
要么
我为读者增加了CommandTimeout.它似乎已经解决了与IDataReader相关的超时问题.
我在代码中做错了吗?
Spo*_*ock 16
您可以在调用WriteToServer之前尝试添加以下内容吗?
bc.BatchSize = 10000;
bc.BulkCopyTimeout = 0;
我不知道默认批量大小或超时是什么,但我怀疑这可能是你的问题.希望有所帮助
此外,您可以尝试使用不同的批量大小来获得最佳性能.
你可以试试这个
bc.BatchSize = 100000; // How many Rows you want to insert at a time
bc.BulkCopyTimeout = 60; // Time in Seconds. If you want infinite waiting Time then assign 0.
| 归档时间: | 
 | 
| 查看次数: | 9761 次 | 
| 最近记录: |