SqlBulkCopyColumnMappingCollection 類別

定義

繼承自 SqlBulkCopyColumnMapping的物件集合CollectionBase

public ref class SqlBulkCopyColumnMappingCollection sealed : System::Collections::CollectionBase
public sealed class SqlBulkCopyColumnMappingCollection : System.Collections.CollectionBase
type SqlBulkCopyColumnMappingCollection = class
    inherit CollectionBase
Public NotInheritable Class SqlBulkCopyColumnMappingCollection
Inherits CollectionBase
繼承
SqlBulkCopyColumnMappingCollection

範例

下列範例會從 AdventureWorks 範例資料庫中的來源資料表,將資料大量複製到相同資料庫中的目的地資料表。 雖然目的地的欄位數與來源欄位數相符,但欄位名稱與序數位置不一致。 SqlBulkCopyColumnMapping加入SqlBulkCopyColumnMappingCollectionSqlBulkCopy物件以建立批量複製的欄位映射。

Important

除非您已如大量複製範例設定中所述建立工作資料表,否則將不會執行此範例。 這個程式碼僅是為了示範使用 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.BulkCopyDemoDifferentColumns;",
                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 (SqlBulkCopy bulkCopy =
                       new SqlBulkCopy(connectionString))
            {
                bulkCopy.DestinationTableName =
                    "dbo.BulkCopyDemoDifferentColumns";

                // The column order in the source doesn't match the order
                // in the destination, so ColumnMappings must be defined.
                bulkCopy.ColumnMappings.Add("ProductID", "ProdID");
                bulkCopy.ColumnMappings.Add("Name", "ProdName");
                bulkCopy.ColumnMappings.Add("ProductNumber", "ProdNum");

                // Write from the source to the destination.
                try
                {
                    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.BulkCopyDemoDifferentColumns;", _
                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 bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString)
                bulkCopy.DestinationTableName = _
                "dbo.BulkCopyDemoDifferentColumns"

                ' The column order in the source doesn't match the order 
                ' in the destination, so ColumnMappings must be defined.
                bulkCopy.ColumnMappings.Add("ProductID", "ProdID")
                bulkCopy.ColumnMappings.Add("Name", "ProdName")
                bulkCopy.ColumnMappings.Add("ProductNumber", "ProdNum")

                ' Write from the source to the destination.
                Try
                    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

備註

欄位映射定義了資料來源與目標資料表之間的對應。

若未定義映射——即 ColumnMappings 集合為空——欄位則根據序數位置隱含映射。 若要執行這項操作,則來源和目標結構描述必須相符。 如果不成功,則擲出。InvalidOperationException

如果集合 ColumnMappings 不是空的,則不需要指定資料來源中所有欄位。 未被集合映射的部分則被忽略。

您可以依名稱或序數找到來源和目標資料行。 你可以在同一個映射集合中混合按名稱和按序數欄位參考。

屬性

名稱 Description
Capacity

取得或設定 可以 CollectionBase 包含的元素數量。

(繼承來源 CollectionBase)
Count

取得該實例中包含 CollectionBase 的元素數量。 此屬性無法覆寫。

(繼承來源 CollectionBase)
InnerList

取得包含實ArrayList例中元素清單的 。CollectionBase

(繼承來源 CollectionBase)
Item[Int32]

取得 SqlBulkCopyColumnMapping 指定索引的物件。

List

取得包含實IList例中元素清單的 。CollectionBase

(繼承來源 CollectionBase)

方法

名稱 Description
Add(Int32, Int32)

建立一個新 SqlBulkCopyColumnMapping 欄位並加入集合,使用序數來指定來源欄和目的欄。

Add(Int32, String)

建立一個新 SqlBulkCopyColumnMapping 資料並加入集合,來源欄位使用序數,目的欄位使用字串。

Add(SqlBulkCopyColumnMapping)

將指定的映射加入 SqlBulkCopyColumnMappingCollection

Add(String, Int32)

建立一個新 SqlBulkCopyColumnMapping 欄位並將其加入集合,使用欄位名稱描述來源欄位,並以序數指定目標欄位。

Add(String, String)

建立一個新 SqlBulkCopyColumnMapping 欄位並加入集合,使用欄位名稱指定來源欄位與目的欄位。

Clear()

清除收藏內容。

Contains(SqlBulkCopyColumnMapping)

會取得一個值,表示集合中是否存在指定 SqlBulkCopyColumnMapping 物件。

CopyTo(SqlBulkCopyColumnMapping[], Int32)

將 的 SqlBulkCopyColumnMappingCollection 元素複製到一個從特定索引開始的項目陣列 SqlBulkCopyColumnMapping

Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

回傳一個枚舉器,會遍歷該 CollectionBase 實例。

(繼承來源 CollectionBase)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IndexOf(SqlBulkCopyColumnMapping)

取得指定 SqlBulkCopyColumnMapping 物件的索引。

Insert(Int32, SqlBulkCopyColumnMapping)

在指定的索引處插入新SqlBulkCopyColumnMapping

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnClear()

在清除實例內容 CollectionBase 時執行額外的自訂程序。

(繼承來源 CollectionBase)
OnClearComplete()

清除實例內容 CollectionBase 後,執行額外的自訂程序。

(繼承來源 CollectionBase)
OnInsert(Int32, Object)

在插入新元素前 CollectionBase ,執行額外的自訂程序。

(繼承來源 CollectionBase)
OnInsertComplete(Int32, Object)

插入新元素後 CollectionBase 執行額外的自訂程序。

(繼承來源 CollectionBase)
OnRemove(Int32, Object)

移除實例中的 CollectionBase 元素時,會執行額外的自訂程序。

(繼承來源 CollectionBase)
OnRemoveComplete(Int32, Object)

移除實 CollectionBase 例元素後,執行額外的自訂程序。

(繼承來源 CollectionBase)
OnSet(Int32, Object, Object)

在設定實例值 CollectionBase 前,執行額外的自訂程序。

(繼承來源 CollectionBase)
OnSetComplete(Int32, Object, Object)

在實例設定值 CollectionBase 後,執行額外的自訂程序。

(繼承來源 CollectionBase)
OnValidate(Object)

在驗證值時執行額外的自訂流程。

(繼承來源 CollectionBase)
Remove(SqlBulkCopyColumnMapping)

從 中移除指定的 SqlBulkCopyColumnMapping 元素 SqlBulkCopyColumnMappingCollection

RemoveAt(Int32)

從集合中移除指定索引的映射。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
ICollection.CopyTo(Array, Int32)

從目標陣列指定的索引開始,將整個 CollectionBase 複製到相容的一維 Array

(繼承來源 CollectionBase)
ICollection.IsSynchronized

取得值,指出是否同步存取 CollectionBase (線程安全)。

(繼承來源 CollectionBase)
ICollection.SyncRoot

取得一個物件,可用來同步存取 CollectionBase

(繼承來源 CollectionBase)
IList.Add(Object)

在 的末尾 CollectionBase加上一個物件。

(繼承來源 CollectionBase)
IList.Contains(Object)

判斷是否 CollectionBase 包含特定元素。

(繼承來源 CollectionBase)
IList.IndexOf(Object)

搜尋指定的 Object ,並返回整個 CollectionBase中首次出現的零基索引。

(繼承來源 CollectionBase)
IList.Insert(Int32, Object)

在指定的索引處插入一個元素 CollectionBase

(繼承來源 CollectionBase)
IList.IsFixedSize

會得到一個值,表示 是否 CollectionBase 具有固定大小。

(繼承來源 CollectionBase)
IList.IsReadOnly

取得值,指出 CollectionBase 是否為唯讀。

(繼承來源 CollectionBase)
IList.Item[Int32]

取得或設定位於指定索引處的專案。

(繼承來源 CollectionBase)
IList.Remove(Object)

移除特定物件 CollectionBase首次出現的 。

(繼承來源 CollectionBase)

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於

另請參閱