SqlBulkCopy.BatchSize Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Sorok száma az egyes kötegekben. Az egyes kötegek végén a köteg sorait a rendszer elküldi a kiszolgálónak.
public:
property int BatchSize { int get(); void set(int value); };
public int BatchSize { get; set; }
member this.BatchSize : int with get, set
Public Property BatchSize As Integer
Tulajdonság értéke
A tulajdonság egészértéke BatchSize , vagy nulla, ha nincs beállítva érték.
Példák
Az alábbi konzolalkalmazás bemutatja, hogyan tölthetők be tömegesen az adatok 50 sorból álló kötegekben. Egy tranzakció működését szemléltető BatchSize példát a Tranzakció és a Tömeges másolási műveletek című témakörben talál.
Ebben a példában a forrásadatok először egy SQL Server táblából egy SqlDataReader példányba olvasnak be. A forrásadatoknak nem kell a SQL Server találhatónak lenniük; bármely olyan adatforrást használhat, amely beolvasható egy IDataReader vagy betölthető egy DataTable.
Important
Ez a példa nem fog futni, hacsak nem hozta létre a munkatáblákat a Tömeges másolási példa beállításaszerint. Ez a kód csak az SqlBulkCopy használatának szintaxisát mutatja be. Ha a forrás- és céltáblák ugyanabban a SQL Server példányban találhatók, egyszerűbb és gyorsabb egy Transact-SQL INSERT ... SELECT utasítás használata az adatok másolásához.
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader();
// Create the SqlBulkCopy object using a connection string.
// In the real world you would not use SqlBulkCopy to move
// data from one table to the other in the same database.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
// Set the BatchSize.
bulkCopy.BatchSize = 50;
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Close the SqlDataReader. The SqlBulkCopy
// object is automatically closed at the end
// of the using block.
reader.Close();
}
// 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);
Console.WriteLine("{0} rows were added.", countEnd - countStart);
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
}
}
private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim connectionString As String = GetConnectionString()
' Open a connection to the AdventureWorks database.
Using sourceConnection As SqlConnection = _
New SqlConnection(connectionString)
sourceConnection.Open()
' Perform an initial count on the destination table.
Dim commandRowCount As New SqlCommand( _
"SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", _
sourceConnection)
Dim countStart As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Starting row count = {0}", countStart)
' Get data from the source table as a SqlDataReader.
Dim commandSourceData As SqlCommand = New SqlCommand( _
"SELECT ProductID, Name, ProductNumber " & _
"FROM Production.Product;", sourceConnection)
Dim reader As SqlDataReader = commandSourceData.ExecuteReader
' Create the SqlBulkCopy object using a connection string.
' In the real world you would not use SqlBulkCopy to move
' data from one table to the other in the same database.
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(connectionString)
bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"
' Set the BatchSize.
bulkCopy.BatchSize = 50
Try
' Write from the source to the destination.
bulkCopy.WriteToServer(reader)
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
' Close the SqlDataReader. The SqlBulkCopy
' object is automatically closed at the end
' of the Using block.
reader.Close()
End Try
End Using
' Perform a final count on the destination table
' to see how many rows were added.
Dim countEnd As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Ending row count = {0}", countEnd)
Console.WriteLine("{0} rows were added.", countEnd - countStart)
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Using
End Sub
Private Function GetConnectionString() As String
' To avoid storing the sourceConnection string in your code,
' you can retrieve it from a configuration file.
Return "Data Source=(local);" & _
"Integrated Security=true;" & _
"Initial Catalog=AdventureWorks;"
End Function
End Module
Megjegyzések
A kötegek akkor fejeződnek be, ha BatchSize sorokat dolgoztak fel, vagy nincs több olyan sor, amelyet a céladatforrásba küldenének.
A nulla (alapértelmezett) azt jelzi, hogy minden WriteToServer művelet egyetlen köteg.
Ha a példányt a SqlBulkCopyUseInternalTransaction beállítás nélkül deklarálták, a rendszer egyszerre küld sorokat a kiszolgáló BatchSize soraiba, de nem hajt végre tranzakcióval kapcsolatos műveletet. Ha UseInternalTransaction érvényben van, minden sorköteg külön tranzakcióként lesz beszúrva.
A BatchSize tulajdonság bármikor beállítható. Ha egy tömeges másolat már folyamatban van, az aktuális köteg mérete az előző kötegméretnek megfelelően van méretezve. A későbbi kötegek az új méretet használják. Ha a BatchSize művelet kezdetben nulla, és egy WriteToServer művelet már folyamatban van, az adott művelet egyetlen kötegként tölti be az adatokat. Az ugyanazon WriteToServer példányon végzett további SqlBulkCopy műveletek az újat BatchSizehasználják.