Sprache
Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Die einfachste Herangehensweise an einen SQL Server-Massenkopiervorgang ist das Durchführen eines einzelnen Vorgangs für eine Datenbank. Standardmäßig wird ein Massenkopiervorgang als isolierter Vorgang ausgeführt: Der Kopiervorgang erfolgt in nicht transaktionaler Weise, ohne Möglichkeit zum Rollback.
Hinweis
Wenn Sie einen Rollback aller oder eines Teils einer Massenkopie nach einem Fehler ausführen möchten, geben Sie SqlBulkCopyOptions.UseInternalTransaction für Transaktionen pro Batch an, oder führen Sie die Kopie innerhalb einer vorhandenen Transaktion aus.
SqlBulkCopy funktioniert auch mit System.Transactions, wenn die Verbindung implizit oder explizit eingebunden wird.
Weitere Informationen finden Sie unter Transaktionen und Massenkopiervorgänge.
Dies sind die allgemeinen Schritte zum Durchführen eines Massenkopiervorgangs:
Herstellen einer Verbindung mit dem Quellserver und Abrufen der zu kopierenden Daten. Die Daten können auch aus einer anderen Quelle stammen, solange sie aus einem IDataReader- oder DataTable-Objekt abgerufen werden können.
Stellen Sie eine Verbindung mit dem Zielserver her, es sei denn, Sie möchten, dass
SqlBulkCopydie Verbindung für Sie herstellt.Erstellen Sie das SqlBulkCopy Objekt, und legen Sie die erforderlichen Eigenschaften fest.
Legen Sie die
DestinationTableNameEigenschaft fest, um die Zieltabelle für den Masseneinfügevorgang anzugeben.Rufen Sie eine der
WriteToServerMethoden auf.Aktualisieren Sie bei Bedarf die Eigenschaften und rufen Sie
WriteToServererneut auf.Rufen sie Close auf, oder umschließen Sie die Massenkopiervorgänge mit einer
Using-Anweisung.
Achtung
Verwenden Sie übereinstimmende Quell- und Zielspaltendatentypen. Wenn die Typen nicht übereinstimmen, versucht SqlBulkCopy, jeden Quellwert mit den Regeln für Value in den Zieldatentyp zu konvertieren. Konvertierungen können sich auf die Leistung auswirken und unerwartete Fehler verursachen. Beispielsweise kann die Konvertierung von Double in Decimal für einige Werte fehlschlagen.
Beispiel
Die folgende Konsolenanwendung veranschaulicht, wie Daten mit der SqlBulkCopy Klasse geladen werden. Im Beispiel werden daten SqlDataReader aus Production.Product der SQL Server-Datenbank AdventureWorks in eine ähnliche Tabelle in derselben Datenbank kopiert.
Wichtig
Erstellen Sie vor dem Ausführen dieses Beispiels die Arbeitstabellen, die im Bulk Copy Example Setup beschrieben sind. Das Beispiel veranschaulicht die SqlBulkCopy Syntax. Wenn sich die Quell- und Zieltabellen in derselben SQL Server Instanz befinden, verwenden Sie eine Transact-SQL -Anweisung (T-SQL) INSERT ... SELECT für eine schnellere Kopie.
static void Main()
{
var connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new(connectionString))
{
sourceConnection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine($"Starting row count = {countStart}");
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader();
// Open the destination connection. In the real world you would
// not use SqlBulkCopy to move data from one table to the other
// in the same database. This is for demonstration purposes only.
using (SqlConnection destinationConnection =
new(connectionString))
{
destinationConnection.Open();
// Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
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 = Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine($"Ending row count = {countEnd}");
Console.WriteLine($"{countEnd - countStart} rows were added.");
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
}
}
Imports Microsoft.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 New SqlCommand( _
"SELECT ProductID, Name, ProductNumber " & _
"FROM Production.Product;", sourceConnection)
Dim reader As SqlDataReader = commandSourceData.ExecuteReader
' Open the destination connection. In the real world you would
' not use SqlBulkCopy to move data from one table to the other
' in the same database. This is for demonstration purposes only.
Using destinationConnection As SqlConnection = _
New SqlConnection(connectionString)
destinationConnection.Open()
' Set up the bulk copy object.
' The column positions in the source data reader
' match the column positions in the destination table,
' so there is no need to map columns.
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(destinationConnection)
bulkCopy.DestinationTableName = _
"dbo.BulkCopyDemoMatchingColumns"
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 Using
End Sub
Private Function GetConnectionString() As String
Throw New NotImplementedException()
End Function
End Module
Durchführen eines Massenkopiervorgangs mithilfe von Transact-SQL und der Command-Klasse
Das folgende Beispiel veranschaulicht, wie die Methode ExecuteNonQuery verwendet wird, um die Anweisung BULK INSERT auszuführen.
Hinweis
Der Dateipfad für die Datenquelle ist relativ zum Server. Der Serverprozess muss Zugriff auf diesen Pfad besitzen, damit der Massenkopiervorgang erfolgreich ausgeführt werden kann.
Using connection As SqlConnection = New SqlConnection(connectionString)
Dim queryString As String = _
"BULK INSERT Northwind.dbo.[Order Details] FROM " & _
"'f:\mydata\data.tbl' WITH (FORMATFILE='f:\mydata\data.fmt' )"
connection.Open()
Dim command As New SqlCommand(queryString, connection)
command.ExecuteNonQuery()
End Using
using (SqlConnection connection = new SqlConnection(connectionString))
{
string queryString = "BULK INSERT Northwind.dbo.[Order Details] " +
@"FROM 'f:\mydata\data.tbl' " +
@"WITH ( FORMATFILE='f:\mydata\data.fmt' )";
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery();
}