SqlBulkCopy Konstruktoren

Definition

Initialisiert eine neue Instanz der SqlBulkCopy-Klasse.

Überlädt

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)

Initialisiert und öffnet auf Grundlage des angegebenen connectionString eine neue Instanz von SqlConnection. Der Konstruktor initialisiert mithilfe von SqlConnection eine neue Instanz der SqlBulkCopy-Klasse. Die SqlConnection-Instanz verhält sich entsprechend den Optionen, die im copyOptions-Parameter angegeben wurden.

SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction)

Initialisiert mit der angegebenen geöffneten Instanz von SqlBulkCopy eine neue Instanz der SqlConnection-Klasse. Die SqlBulkCopy-Instanz verhält sich entsprechend den Optionen, die im copyOptions-Parameter angegeben wurden. Wenn eine SqlTransaction ungleich Null angegeben wird, erfolgen die Kopiervorgänge innerhalb dieser Transaktion.

SqlBulkCopy(SqlConnection)

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, mit der Massenkopiervorgänge ausgeführt werden. Wenn Ihre Verbindungszeichenfolge Integrated Security = true nicht verwendet, können Sie SqlCredential verwenden, um die Benutzer-ID und das Kennwort sicherer zu übergeben als durch die Angabe von Benutzer-ID und Kennwort als Text in der Verbindungszeichenfolge.

Beispiele

Die folgende Konsolenanwendung veranschaulicht das Massenladen von Daten mithilfe einer bereits geöffneten Verbindung. In diesem Beispiel werden in der SQL Server-Datenbank AdventureWorks unter Verwendung von SqlDataReader Daten aus der Tabelle Production.Product in eine ähnliche Tabelle derselben Datenbank kopiert. Dieses Beispiel dient ausschließlich Demonstrationszwecken. Sie würden nicht verwenden SqlBulkCopy , um Daten aus einer Tabelle in eine andere in derselben Datenbank in einer Produktionsanwendung zu 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 gelesen oder in eine DataTablegeladen werden kann.

Wichtig

Dieses Beispiel wird nur ausgeführt, wenn Sie die Arbeitstabellen zuvor wie unter Massenkopierbeispiel-Einrichtung 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 instance 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 der SqlBulkCopy instance initialisiert wird, bleibt die Verbindung geöffnet, nachdem die SqlBulkCopy instance geschlossen wurde.

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

Weitere Informationen

Gilt für:

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.

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 zur Definition der Verbindung, die für die SqlBulkCopy-Instanz geöffnet wird. Wenn Ihre Verbindungszeichenfolge Integrated Security = true nicht 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 durch die Angabe von Benutzer-ID und Kennwort als Text in der Verbindungszeichenfolge.

Beispiele

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

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

Wichtig

Dieses Beispiel wird nur ausgeführt, wenn Sie die Arbeitstabellen zuvor wie unter Massenkopierbeispiel-Einrichtung 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 instance 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 Massenkopiervorgangs automatisch geschlossen.

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

Weitere Informationen

Gilt für:

SqlBulkCopy(String, SqlBulkCopyOptions)

Initialisiert und öffnet auf Grundlage des angegebenen connectionString eine neue Instanz von SqlConnection. Der Konstruktor initialisiert mithilfe von SqlConnection eine neue Instanz der SqlBulkCopy-Klasse. Die SqlConnection-Instanz verhält sich entsprechend den Optionen, die im copyOptions-Parameter angegeben wurden.

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 zur Definition der Verbindung, die für die SqlBulkCopy-Instanz geöffnet wird. Wenn Ihre Verbindungszeichenfolge Integrated Security = true nicht 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 durch die Angabe von Benutzer-ID und Kennwort als Text in der Verbindungszeichenfolge.

copyOptions
SqlBulkCopyOptions

Eine Kombination von Werten aus der SqlBulkCopyOptions-Enumeration, die festlegt, welche Zeilen der Datenquelle in die Zieltabelle kopiert werden.

Beispiele

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

Wichtig

Dieses Beispiel wird nur ausgeführt, wenn Sie die Arbeitstabellen zuvor wie unter Massenkopierbeispiel-Einrichtung 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 instance 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

Sie können detaillierte Informationen zu allen Massenkopieroptionen im SqlBulkCopyOptions Thema erhalten.

Weitere Informationen

Gilt für:

SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction)

Initialisiert mit der angegebenen geöffneten Instanz von SqlBulkCopy eine neue Instanz der SqlConnection-Klasse. Die SqlBulkCopy-Instanz verhält sich entsprechend den Optionen, die im copyOptions-Parameter angegeben wurden. Wenn eine SqlTransaction ungleich Null angegeben wird, erfolgen die Kopiervorgänge innerhalb dieser Transaktion.

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, mit der der Massenkopiervorgang ausgeführt wird. Wenn Ihre Verbindungszeichenfolge Integrated Security = truenicht verwendet, können Sie SqlCredential verwenden, um die Benutzer-ID und das Kennwort sicherer zu übergeben als durch die Angabe von Benutzer-ID und Kennwort als Text in der Verbindungszeichenfolge.

copyOptions
SqlBulkCopyOptions

Eine Kombination von Werten aus der SqlBulkCopyOptions-Enumeration, die festlegt, welche Zeilen der Datenquelle in die Zieltabelle kopiert werden.

externalTransaction
SqlTransaction

Eine vorhandene SqlTransaction-Instanz, unter der der Massenkopiervorgang 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 Massenkopiervorgänge.

Weitere Informationen

Gilt für: