Вопрос

I wrote some payroll processing software which ultimately writes data out to a SQL Server table using SqlDataAdapter.Insert(). (I am using the default Insert command created with SqlCommandBuilder().) I am using the .NET Framework 4 on Windows 7, writing to SQL Server 2008 (not R2) Std Edition x64 SP3. The problem is, I am only seeing about 2 Mbps throughput from my PC (Dell Optiplex 990 Core i7) to the SQL Server (NICs on my PC and the server are GB Enet but there are some 100Mbps switches in between for an unfortunate bottleneck, but still - 100Mbps). I can copy a file from my PC to the server where SQL Server is running and I have no throughput problems - I can get very close to 100 Mbps throughput there. I put the database on another LAN SQL Server running 2008 R2 and there I manage to get 5 Mbps throughput, but still - very slow. My PC's CPU is near 0 when the Insert() command is running. I tried to increase and decrease the Packet Size parameter in the SqlConnection.ConnectionString property, but that did nothing.

If I perform a SELECT * FROM <table> from SSMS on my PC to the SQL Server in question, I can get about 45 Mbps sustained throughput.

I'm not sure where to look next for the culprit - any suggestions appreciated.

Это было полезно?

Решение

SqlDataAdapter's Insert does its records one at a time, if you are inserting a lot of records you should be using SqlBulkCopy instead to mitigate the overhead of doing those inserts.

using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
    destinationConnection.Open();
    using (SqlBulkCopy bulkCopy =
               new SqlBulkCopy(destinationConnection))
    {
        bulkCopy.DestinationTableName = destTableName;
        bulkCopy.WriteToServer(sourceDataTable);
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top