SqlBulkCopyOptions 列舉

定義

位元旗標,其指定要和 SqlBulkCopy 執行個體使用的一或多個選項。

此列舉支援其成員值的位元組合。

public enum class SqlBulkCopyOptions
[System.Flags]
public enum SqlBulkCopyOptions
[<System.Flags>]
type SqlBulkCopyOptions = 
Public Enum SqlBulkCopyOptions
繼承
SqlBulkCopyOptions
屬性

欄位

AllowEncryptedValueModifications 64

指定時, AllowEncryptedValueModifications 可讓您在數據表或資料庫之間大量複製加密的數據,而不需解密數據。 一般而言,應用程式會從一個資料表的加密資料行選取資料而不會解密資料 (應用程式會連線到資料庫,並將資料行加密設定關鍵字設定為停用),接著會使用這個選項來大量插入資料,資料仍然加密。 如需詳細資訊,請參閱 Always Encrypted

指定 AllowEncryptedValueModifications 時請小心,因為這可能會導致資料庫損毀,因為驅動程式不會檢查數據是否確實加密,或者是否使用與目標數據行相同的加密類型、演算法和密鑰正確加密。

CheckConstraints 2

在插入資料時檢查條件約束。 根據預設,不會檢查條件約束。

Default 0

使用所有選項的預設值。

FireTriggers 16

若已指定,則會導致此伺服器對於正在插入至此資料庫的資料列,引發插入觸發程序。

KeepIdentity 1

保留來源識別值。 如果未指定,則識別值依目的地指派。

KeepNulls 8

不論預設值的設定為何,均保留目的地資料表中的 null 值。 如果未指定,則 null 值會以適用的預設值取代。

TableLock 4

在大量複製作業期間,取得大量更新鎖定。 如果未指定,則會使用資料列鎖定。

UseInternalTransaction 32

若已指定,則大量複製作業的每個批次將在交易內發生。 如果您指定這個選項,同時也提供 SqlTransaction 物件給建構函式,則 ArgumentException 就會發生。

範例

下列主控台應用程式示範如何執行大量載入,將源資料表的識別資料行中的值複製到目的地資料表中的對應資料行,而不是為每個數據列的識別數據行產生新的值。

若要查看選項如何變更大量載入的運作方式,請使用 dbo 執行範例。BulkCopyDemoMatchingColumns 數據表空白。 從來源載入所有數據列。 接下來,再次執行範例,而不用清空數據表。 擲回例外狀況,而程式代碼會將訊息寫入控制台視窗,通知您因為主鍵違規而未新增數據列。

重要

除非您已如大量複製範例設定中所述建立工作資料表,否則將不會執行此範例。 這個程式碼僅是為了示範使用 SqlBulkCopy 的語法而提供。 如果來源和目的地數據表位於相同的 SQL Server 實例中,使用 Transact-SQL INSERT … SELECT 語句來複製數據會比較簡單且更快速。

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

備註

當您建構 SqlBulkCopy 實例以變更WriteToServer該實例的方法行為時,您可以使用 SqlBulkCopyOptions 列舉。

適用於

另請參閱