SqlBulkCopy Konstruktoren

Definition

Initialisiert eine neue Instanz der SqlBulkCopy-Klasse.

Überlädt

Name Beschreibung
SqlBulkCopy(SqlConnection)

Hiermit wird eine neue Instanz der SqlBulkCopy-Klasse mithilfe der angegebenen geöffneten Instanz von SqlConnection initialisiert.

SqlBulkCopy(String)

Hiermit wird eine neue Instanz von SqlConnection auf Grundlage der angegebenen connectionString initialisiert und geöffnet. Im Konstruktor wird die SqlConnection verwendet, um eine neue Instanz der SqlBulkCopy-Klasse zu initialisieren.

SqlBulkCopy(String, SqlBulkCopyOptions)

Hiermit wird eine neue Instanz von SqlConnection auf Grundlage der angegebenen connectionString initialisiert und geöffnet. Der Konstruktor verwendet diese SqlConnection , um eine neue Instanz der SqlBulkCopy Klasse zu initialisieren. Die SqlConnection Instanz verhält sich entsprechend den optionen, die copyOptions im Parameter angegeben sind.

SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction)

Initialisiert eine neue Instanz der SqlBulkCopy Klasse mithilfe der bereitgestellten geöffneten Instanz von SqlConnection. Die SqlBulkCopy Instanz verhält sich entsprechend den optionen, die copyOptions im Parameter angegeben sind. Wenn ein Nicht-Null-Wert SqlTransaction angegeben wird, werden die Kopiervorgänge innerhalb dieser Transaktion ausgeführt.

SqlBulkCopy(SqlConnection)

Quelle:
System.Data.SqlClient.notsupported.cs

Hiermit wird eine neue Instanz der SqlBulkCopy-Klasse mithilfe der angegebenen geöffneten Instanz von SqlConnection initialisiert.

public:
 SqlBulkCopy(System::Data::SqlClient::SqlConnection ^ connection);
public SqlBulkCopy(System.Data.SqlClient.SqlConnection connection);
new System.Data.SqlClient.SqlBulkCopy : System.Data.SqlClient.SqlConnection -> System.Data.SqlClient.SqlBulkCopy
Public Sub New (connection As SqlConnection)

Parameter

connection
SqlConnection

Die bereits geöffnete SqlConnection Instanz, die zum Ausführen des Massenkopievorgangs verwendet wird. Wenn Ihre Verbindungszeichenfolge nicht verwendet Integrated Security = truewird, können SqlCredential Sie die Benutzer-ID und das Kennwort sicherer übergeben, als indem Sie die Benutzer-ID und das Kennwort als Text in der Verbindungszeichenfolge angeben.

Beispiele

Die folgende Konsolenanwendung veranschaulicht das Massenladen von Daten mithilfe einer bereits geöffneten Verbindung. In diesem Beispiel werden in der SQL Server-Datenbank SqlDataReader unter Verwendung von Daten aus der Tabelle Production.Product in eine ähnliche Tabelle derselben Datenbank kopiert. Dieses Beispiel dient nur zu Demonstrationszwecken. Sie würden daten nicht SqlBulkCopy aus einer Tabelle in dieselbe Datenbank in einer Produktionsanwendung verschieben. Beachten Sie, dass sich die Quelldaten nicht auf SQL Server befinden müssen; Sie können eine beliebige Datenquelle verwenden, die in eine IDataReader oder geladen werden DataTablekann.

Important

Dieses Beispiel wird nur ausgeführt, wenn Sie die Arbeitstabellen zuvor wie unter Beispiel für die Einrichtung des Massenkopierens beschrieben erstellt haben. Der angegebene Code dient nur zur Demonstration der Syntax für die Verwendung von SqlBulkCopy. Wenn sich die Quell- und Zieltabellen in derselben SQL Server-Instanz befinden, ist es einfacher und schneller, eine Transact-SQL-Anweisung INSERT ... SELECT zum Kopieren der Daten zu verwenden.

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();

            // 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 SqlConnection(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 SqlBulkCopy(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 = 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 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
        ' 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

Hinweise

Da die Verbindung bereits geöffnet ist, wenn die SqlBulkCopy Instanz initialisiert wird, bleibt die Verbindung geöffnet, nachdem die SqlBulkCopy Instanz geschlossen wurde.

Wenn das connection Argument null ist, wird ein ArgumentNullException Fehler ausgelöst.

Weitere Informationen

Gilt für:

SqlBulkCopy(String)

Quelle:
System.Data.SqlClient.notsupported.cs

Hiermit wird eine neue Instanz von SqlConnection auf Grundlage der angegebenen connectionString initialisiert und geöffnet. Im Konstruktor wird die SqlConnection verwendet, um eine neue Instanz der SqlBulkCopy-Klasse zu initialisieren.

public:
 SqlBulkCopy(System::String ^ connectionString);
public SqlBulkCopy(string connectionString);
new System.Data.SqlClient.SqlBulkCopy : string -> System.Data.SqlClient.SqlBulkCopy
Public Sub New (connectionString As String)

Parameter

connectionString
String

Die Zeichenfolge, die die Verbindung definiert, die für die Verwendung durch die SqlBulkCopy Instanz geöffnet wird. Wenn Ihr Verbindungszeichenfolge nicht Integrated Security = true verwendet, können Sie SqlBulkCopy(SqlConnection) oder SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction) und SqlCredential verwenden, um die Benutzer-ID und das Kennwort sicherer zu übergeben, als die Benutzer-ID und das Kennwort als Text im Verbindungszeichenfolge anzugeben.

Beispiele

Die folgende Konsolenanwendung veranschaulicht das Massenladen von Daten mithilfe einer als Zeichenfolge angegebenen Verbindung. Die Verbindung wird automatisch geschlossen, wenn die SqlBulkCopy Instanz geschlossen wird.

In diesem Beispiel werden die Quelldaten zuerst aus einer SQL Server-Tabelle in eine SqlDataReader Instanz gelesen. Die Quelldaten müssen sich nicht auf SQL Server befinden. Sie können eine beliebige Datenquelle verwenden, die in eine IDataReader oder geladen werden DataTablekann.

Important

Dieses Beispiel wird nur ausgeführt, wenn Sie die Arbeitstabellen zuvor wie unter Beispiel für die Einrichtung des Massenkopierens beschrieben erstellt haben. Der angegebene Code dient nur zur Demonstration der Syntax für die Verwendung von SqlBulkCopy. Wenn sich die Quell- und Zieltabellen in derselben SQL Server-Instanz befinden, ist es einfacher und schneller, eine Transact-SQL-Anweisung INSERT ... SELECT zum Kopieren der Daten zu verwenden.

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();

            // Set up the bulk copy 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";

                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

            ' Set up the bulk copy 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"

                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

Hinweise

Die Verbindung wird am Ende des Massenkopievorgangs automatisch geschlossen.

Wenn connectionString null ist, wird ein ArgumentNullException Fehler ausgelöst. Wenn connectionString es sich um eine leere Zeichenfolge handelt, wird eine ArgumentException ausgelöst.

Weitere Informationen

Gilt für:

SqlBulkCopy(String, SqlBulkCopyOptions)

Quelle:
System.Data.SqlClient.notsupported.cs

Hiermit wird eine neue Instanz von SqlConnection auf Grundlage der angegebenen connectionString initialisiert und geöffnet. Der Konstruktor verwendet diese SqlConnection , um eine neue Instanz der SqlBulkCopy Klasse zu initialisieren. Die SqlConnection Instanz verhält sich entsprechend den optionen, die copyOptions im Parameter angegeben sind.

public:
 SqlBulkCopy(System::String ^ connectionString, System::Data::SqlClient::SqlBulkCopyOptions copyOptions);
public SqlBulkCopy(string connectionString, System.Data.SqlClient.SqlBulkCopyOptions copyOptions);
new System.Data.SqlClient.SqlBulkCopy : string * System.Data.SqlClient.SqlBulkCopyOptions -> System.Data.SqlClient.SqlBulkCopy
Public Sub New (connectionString As String, copyOptions As SqlBulkCopyOptions)

Parameter

connectionString
String

Die Zeichenfolge, die die Verbindung definiert, die für die Verwendung durch die SqlBulkCopy Instanz geöffnet wird. Wenn Ihr Verbindungszeichenfolge nicht Integrated Security = true verwendet, können Sie SqlBulkCopy(SqlConnection) oder SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction) und SqlCredential verwenden, um die Benutzer-ID und das Kennwort sicherer zu übergeben, als die Benutzer-ID und das Kennwort als Text im Verbindungszeichenfolge anzugeben.

copyOptions
SqlBulkCopyOptions

Eine Kombination von Werten aus der SqlBulkCopyOptions Enumeration, die bestimmt, welche Datenquellenzeilen in die Zieltabelle kopiert werden.

Beispiele

Die folgende Konsolenanwendung veranschaulicht, wie Sie eine Massenladevorgang mithilfe einer als Zeichenfolge angegebenen Verbindung ausführen. Wenn Sie die Zieltabelle laden, wird eine Option festgelegt, um den Wert in der Identitätsspalte der Quelltabelle zu verwenden. In diesem Beispiel werden die Quelldaten zuerst aus einer SQL Server-Tabelle in eine SqlDataReader Instanz gelesen. Die Quelltabelle und die Zieltabelle enthalten jeweils eine Identitätsspalte. Standardmäßig wird ein neuer Wert für die Spalte "Identität" in der Zieltabelle für jede hinzugefügte Zeile generiert. In diesem Beispiel wird eine Option festgelegt, wenn die Verbindung geöffnet wird, die den Massenladevorgang erzwingt, stattdessen die Identitätswerte aus der Quelltabelle zu verwenden. Um zu sehen, wie die Option die Funktionsweise des Massenladevorgangs ändert, führen Sie das Beispiel mit dem dbo aus. BulkCopyDemoMatchingColumns-Tabelle leer. Alle Zeilen werden aus der Quelle geladen. Führen Sie dann das Beispiel erneut aus, ohne die Tabelle zu leeren. Eine Ausnahme wird ausgelöst, und der Code schreibt eine Meldung in die Konsole, die Sie darüber informiert, dass Zeilen aufgrund von Verletzungen der Primärschlüsseleinschränkung nicht hinzugefügt wurden.

Important

Dieses Beispiel wird nur ausgeführt, wenn Sie die Arbeitstabellen zuvor wie unter Beispiel für die Einrichtung des Massenkopierens beschrieben erstellt haben. Der angegebene Code dient nur zur Demonstration der Syntax für die Verwendung von SqlBulkCopy. Wenn sich die Quell- und Zieltabellen in derselben SQL Server-Instanz befinden, ist es einfacher und schneller, eine Transact-SQL-Anweisung INSERT ... SELECT zum Kopieren der Daten zu verwenden.

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
            // and the KeepIdentity option.
            // 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, SqlBulkCopyOptions.KeepIdentity))
            {
                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 = 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 
            ' and the KeepIdentity option. 
            ' 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, SqlBulkCopyOptions.KeepIdentity)
                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 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

Hinweise

Detaillierte Informationen zu allen Massenkopieoptionen finden Sie im SqlBulkCopyOptions Thema.

Weitere Informationen

Gilt für:

SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction)

Quelle:
System.Data.SqlClient.notsupported.cs

Initialisiert eine neue Instanz der SqlBulkCopy Klasse mithilfe der bereitgestellten geöffneten Instanz von SqlConnection. Die SqlBulkCopy Instanz verhält sich entsprechend den optionen, die copyOptions im Parameter angegeben sind. Wenn ein Nicht-Null-Wert SqlTransaction angegeben wird, werden die Kopiervorgänge innerhalb dieser Transaktion ausgeführt.

public:
 SqlBulkCopy(System::Data::SqlClient::SqlConnection ^ connection, System::Data::SqlClient::SqlBulkCopyOptions copyOptions, System::Data::SqlClient::SqlTransaction ^ externalTransaction);
public SqlBulkCopy(System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlBulkCopyOptions copyOptions, System.Data.SqlClient.SqlTransaction externalTransaction);
new System.Data.SqlClient.SqlBulkCopy : System.Data.SqlClient.SqlConnection * System.Data.SqlClient.SqlBulkCopyOptions * System.Data.SqlClient.SqlTransaction -> System.Data.SqlClient.SqlBulkCopy
Public Sub New (connection As SqlConnection, copyOptions As SqlBulkCopyOptions, externalTransaction As SqlTransaction)

Parameter

connection
SqlConnection

Die bereits geöffnete SqlConnection Instanz, die zum Ausführen der Massenkopie verwendet wird. Wenn Ihre Verbindungszeichenfolge nicht verwendet Integrated Security = truewird, können SqlCredential Sie die Benutzer-ID und das Kennwort sicherer übergeben, als indem Sie die Benutzer-ID und das Kennwort als Text in der Verbindungszeichenfolge angeben.

copyOptions
SqlBulkCopyOptions

Eine Kombination von Werten aus der SqlBulkCopyOptions Enumeration, die bestimmt, welche Datenquellenzeilen in die Zieltabelle kopiert werden.

externalTransaction
SqlTransaction

Eine vorhandene SqlTransaction Instanz, unter der die Massenkopie ausgeführt wird.

Hinweise

Wenn Optionen enthalten UseInternalTransaction sind und das externalTransaction Argument nicht NULL ist, wird eine InvalidArgumentException ausgelöst.

Beispiele für die Verwendung SqlBulkCopy in einer Transaktion finden Sie unter Transaktions- und Massenkopievorgängen.

Weitere Informationen

Gilt für: