SqlBulkCopyOptions 列舉

定義

指定一或多個選項搭配 實體使用的位旗標,SqlBulkCopy

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

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

欄位

名稱 Description
Default 0

所有選項都使用預設值。

KeepIdentity 1

保留來源識別值。 若未指定,則由目的地分配身份值。

CheckConstraints 2

在插入資料時檢查條件約束。 預設情況下,限制條件不會被檢查。

TableLock 4

在大量複製作業期間,取得大量更新鎖定。 若未特別說明,則使用行鎖。

KeepNulls 8

不論預設值的設定為何,均保留目的地資料表中的 null 值。 若未指定,空值會在適用時被預設值取代。

FireTriggers 16

指定時,伺服器會對入的資料列觸發插入觸發器。

UseInternalTransaction 32

指定時,每個批次的批量複製操作都會發生在交易中。 如果你指定這個選項,並且同時向建構子提供 SqlTransaction 物件,就會發生 a ArgumentException

AllowEncryptedValueModifications 64

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

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

CacheMetadata 128

在指定時, CacheMetadata 會在第一次批量複製操作後快取目標資料表的元資料,讓後續對同一資料表的操作可以跳過元資料發現查詢。 這能提升對同一目的資料表執行多次批量複製操作的效能。

警告: 只有在確定目的資料表架構不會在批量複製操作間改變時,才使用此選項。 若資料表結構改變(新增、移除或修改欄位),使用快取的元資料可能導致資料損壞、操作失敗或異常行為。 如果結構改變,請呼叫 ClearCachedMetadata() 清除快取。

當 被更改到其他資料表時 DestinationTableName ,快取會自動失效。 在操作間切換 ColumnMappings 不需要快取失效,因為快取的中繼資料只描述目的資料表的結構,而非來源到目的的欄位映射。

當連線情境改變時,快取不會自動失效。 若底層 SqlConnection 資料庫(例如via ChangeDatabase(String))變更或因故障轉移而重新連接至其他伺服器,呼叫者應呼叫 ClearCachedMetadata() 以確保元資料已被刷新。

範例

以下主控台應用程式示範如何執行一次批量載入,將來源資料表身份欄位的值複製到目的地資料表對應欄位,而非為每列的身份欄位產生新值。

想看看這個選項如何改變批量裝載的運作方式,可以用 DBO 跑取樣 。BulkCopyDemoMatchingColumns 表格為空。 所有列都是從來源載入。 接著,在不清空表格的情況下再次執行樣本。 當拋出例外時,程式碼會寫入訊息到主控台視窗,通知你因主鍵違規而未新增列數。

這很重要

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

namespace SqlBulkCopy_KeepIdentity;

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實例時使用列SqlBulkCopyOptions舉來改變WriteToServer(DbDataReader)該實例的方法行為。

適用於