SqlBulkCopyOptions 列舉

定義

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

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

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

欄位

AllowEncryptedValueModifications 64

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

指定 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;
using System.Data;
// <Snippet1>
using Microsoft.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;";
    }
}
// </Snippet1>

備註

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

適用於