SqlBulkCopyColumnMappingCollection 類別

定義

繼承自 CollectionBaseSqlBulkCopyColumnMapping 物件集合。Collection of SqlBulkCopyColumnMapping objects that inherits from CollectionBase.

public ref class SqlBulkCopyColumnMappingCollection sealed : System::Collections::CollectionBase
public ref class SqlBulkCopyColumnMappingCollection sealed : System::Collections::IList
public sealed class SqlBulkCopyColumnMappingCollection : System.Collections.CollectionBase
public sealed class SqlBulkCopyColumnMappingCollection : System.Collections.IList
type SqlBulkCopyColumnMappingCollection = class
    inherit CollectionBase
type SqlBulkCopyColumnMappingCollection = class
    interface ICollection
    interface IEnumerable
    interface IList
Public NotInheritable Class SqlBulkCopyColumnMappingCollection
Inherits CollectionBase
Public NotInheritable Class SqlBulkCopyColumnMappingCollection
Implements IList
繼承
SqlBulkCopyColumnMappingCollection
繼承
SqlBulkCopyColumnMappingCollection
實作

範例

下列範例會從 AdventureWorks 範例資料庫中的來源資料表,將資料大量複製到相同資料庫中的目的地資料表。The following example bulk copies data from a source table in the AdventureWorks sample database to a destination table in the same database. 雖然目的地中的資料行數目與來源中的資料行數目相符,但資料行名稱和序數位置不相符。Although the number of columns in the destination matches the number of columns in the source, the column names and ordinal positions do not match. SqlBulkCopyColumnMapping 會加入至 SqlBulkCopyColumnMappingCollection 物件的, SqlBulkCopy 以建立大量複製的資料行對應。SqlBulkCopyColumnMapping are added to the SqlBulkCopyColumnMappingCollection for the SqlBulkCopy object to create a column map for the bulk copy.

重要

除非您已如 大量複製範例設定中所述建立工作資料表,否則此範例不會執行。This sample will not run unless you have created the work tables as described in Bulk Copy Example Setup. 這個程式碼僅是為了示範使用 SqlBulkCopy 的語法而提供。This code is provided to demonstrate the syntax for using SqlBulkCopy only. 如果來源和目的地資料表位於相同的 SQL Server 實例中,則使用 Transact-sql INSERT … SELECT 語句來複製資料會更容易且更快速。If the source and destination tables are in the same SQL Server instance, it is easier and faster to use a Transact-SQL INSERT … SELECT statement to copy the data.

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

備註

資料行對應會定義資料來源與目標資料表之間的對應。Column mappings define the mapping between data source and the target table.

如果未定義對應(也就是, ColumnMappings 集合是空的),則會根據序數位置隱含地對應資料行。If mappings are not defined - that is, the ColumnMappings collection is empty - the columns are mapped implicitly based on ordinal position. 若要執行這項操作,則來源和目標結構描述必須相符。For this to work, source and target schemas must match. 如果沒有, InvalidOperationException 則會擲回。If they do not, an InvalidOperationException is thrown.

如果 ColumnMappings 集合不是空的,則不需要指定資料來源中的每個資料行。If the ColumnMappings collection is not empty, not every column present in the data source has to be specified. 集合未對應的會被忽略。Those not mapped by the collection are ignored.

您可以依名稱或序數找到來源和目標資料行。You can refer to source and target columns by either name or ordinal. 您可以在相同的對應集合中,混用依名稱和依序資料行的參考。You can mix by-name and by-ordinal column references in the same mappings collection.

屬性

Capacity

取得或設定 CollectionBase 可包含的項目數目。Gets or sets the number of elements that the CollectionBase can contain.

(繼承來源 CollectionBase)
Count

取得 SqlBulkCopyColumnMappingCollection 中所包含的項目數。Gets the number of elements contained in the SqlBulkCopyColumnMappingCollection.

Count

取得 CollectionBase 執行個體中包含的元素數目。Gets the number of elements contained in the CollectionBase instance. 這個屬性無法覆寫。This property cannot be overridden.

(繼承來源 CollectionBase)
InnerList

取得包含 ArrayList 執行個體中之元素清單的 CollectionBaseGets an ArrayList containing the list of elements in the CollectionBase instance.

(繼承來源 CollectionBase)
Item[Int32]

取得在指定之索引處的 SqlBulkCopyColumnMapping 物件。Gets the SqlBulkCopyColumnMapping object at the specified index.

List

取得包含 IList 執行個體中之元素清單的 CollectionBaseGets an IList containing the list of elements in the CollectionBase instance.

(繼承來源 CollectionBase)

方法

Add(Int32, Int32)

使用序數指定來源和目的資料行,建立新的 SqlBulkCopyColumnMapping,並將其加入集合。Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using ordinals to specify both source and destination columns.

Add(Int32, String)

對來源資料行使用序數,對目的資料行使用字串,建立新的 SqlBulkCopyColumnMapping,並將其加入集合。Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using an ordinal for the source column and a string for the destination column.

Add(SqlBulkCopyColumnMapping)

將指定對應加入 SqlBulkCopyColumnMappingCollectionAdds the specified mapping to the SqlBulkCopyColumnMappingCollection.

Add(String, Int32)

使用資料行名稱描述來源資料行,並使用序數指定目的資料行,建立新的 SqlBulkCopyColumnMapping,並將其加入集合。Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using a column name to describe the source column and an ordinal to specify the destination column.

Add(String, String)

使用資料行名稱指定來源和目的資料行,建立新的 SqlBulkCopyColumnMapping,並將其加入集合。Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using column names to specify both source and destination columns.

Clear()

清除集合的內容。Clears the contents of the collection.

Contains(SqlBulkCopyColumnMapping)

取得值,指出指定的 SqlBulkCopyColumnMapping 物件是否存在於集合中。Gets a value indicating whether a specified SqlBulkCopyColumnMapping object exists in the collection.

CopyTo(SqlBulkCopyColumnMapping[], Int32)

從特定的陣列索引開始,將 SqlBulkCopyColumnMappingCollection 的元素複製到 SqlBulkCopyColumnMapping 項目的陣列。Copies the elements of the SqlBulkCopyColumnMappingCollection to an array of SqlBulkCopyColumnMapping items, starting at a particular index.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回逐一查看集合的列舉值。Returns an enumerator that iterates through a collection.

GetEnumerator()

傳回可逐一查看 CollectionBase 執行個體的列舉值。Returns an enumerator that iterates through the CollectionBase instance.

(繼承來源 CollectionBase)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(SqlBulkCopyColumnMapping)

取得指定 SqlBulkCopyColumnMapping 物件的索引。Gets the index of the specified SqlBulkCopyColumnMapping object.

Insert(Int32, SqlBulkCopyColumnMapping)

在指定索引處插入新的 SqlBulkCopyColumnMappingInsert a new SqlBulkCopyColumnMapping at the index specified.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnClear()

在清除 CollectionBase 執行個體的內容之後,執行額外的自訂處理序。Performs additional custom processes when clearing the contents of the CollectionBase instance.

(繼承來源 CollectionBase)
OnClearComplete()

在清除 CollectionBase 執行個體的內容後,執行額外的自訂處理序。Performs additional custom processes after clearing the contents of the CollectionBase instance.

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

在將新的元素插入至 CollectionBase 執行個體前,執行額外的自訂處理序。Performs additional custom processes before inserting a new element into the CollectionBase instance.

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

在將新的元素插入至 CollectionBase 執行個體後,執行額外的自訂處理序。Performs additional custom processes after inserting a new element into the CollectionBase instance.

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

當從 CollectionBase 執行個體移除元素時,執行額外的自訂處理序。Performs additional custom processes when removing an element from the CollectionBase instance.

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

在從 CollectionBase 執行個體移除元素後,執行額外的自訂處理序。Performs additional custom processes after removing an element from the CollectionBase instance.

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

CollectionBase 執行個體中設定數值前,執行額外的自訂處理序。Performs additional custom processes before setting a value in the CollectionBase instance.

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

CollectionBase 執行個體中設定數值後,執行額外的自訂處理序。Performs additional custom processes after setting a value in the CollectionBase instance.

(繼承來源 CollectionBase)
OnValidate(Object)

當驗證數值時,執行額外的自訂處理序。Performs additional custom processes when validating a value.

(繼承來源 CollectionBase)
Remove(SqlBulkCopyColumnMapping)

將指定的 SqlBulkCopyColumnMapping 項目從 SqlBulkCopyColumnMappingCollection 移除。Removes the specified SqlBulkCopyColumnMapping element from the SqlBulkCopyColumnMappingCollection.

RemoveAt(Int32)

移除集合指定索引處的對應。Removes the mapping at the specified index from the collection.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從特定的 ICollection 索引開始,將 Array 的項目複製到 ArrayCopies the elements of the ICollection to an Array, starting at a particular Array index.

ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 CollectionBase 複製到相容的一維 ArrayCopies the entire CollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(繼承來源 CollectionBase)
ICollection.IsSynchronized

取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。Gets a value indicating whether access to the ICollection is synchronized (thread safe).

ICollection.IsSynchronized

取得值,這個值表示對 CollectionBase 的存取是否同步 (安全執行緒)。Gets a value indicating whether access to the CollectionBase is synchronized (thread safe).

(繼承來源 CollectionBase)
ICollection.SyncRoot

取得可用以同步存取 ICollection 的物件。Gets an object that can be used to synchronize access to the ICollection.

ICollection.SyncRoot

取得可用以同步存取 CollectionBase 的物件。Gets an object that can be used to synchronize access to the CollectionBase.

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

將項目加入至 IListAdds an item to the IList.

IList.Add(Object)

將物件加入至 CollectionBase 的末端。Adds an object to the end of the CollectionBase.

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

判斷 IList 是否包含特定值。Determines whether the IList contains a specific value.

IList.Contains(Object)

判斷 CollectionBase 是否包含特定項目。Determines whether the CollectionBase contains a specific element.

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

判斷 IList 中指定項目的索引。Determines the index of a specific item in the IList.

IList.IndexOf(Object)

搜尋指定的 Object,並傳回在整個 CollectionBase 中第一個符合項目之以零為起始的索引。Searches for the specified Object and returns the zero-based index of the first occurrence within the entire CollectionBase.

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

將項目插入位於指定索引的 IListInserts an item to the IList at the specified index.

IList.Insert(Int32, Object)

將項目插入至 CollectionBase 中指定的索引位置。Inserts an element into the CollectionBase at the specified index.

(繼承來源 CollectionBase)
IList.IsFixedSize

取得值,該值指出 IList 是否具有固定的大小。Gets a value indicating whether the IList has a fixed size.

IList.IsFixedSize

取得值,指出 CollectionBase 是否有固定的大小。Gets a value indicating whether the CollectionBase has a fixed size.

(繼承來源 CollectionBase)
IList.IsReadOnly

取得值,指出 IList 是否唯讀。Gets a value indicating whether the IList is read-only.

IList.IsReadOnly

取得值,指出 CollectionBase 是否唯讀。Gets a value indicating whether the CollectionBase is read-only.

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

在指定的索引位置上取得或設定項目。Gets or sets the element at the specified index.

IList.Item[Int32]

在指定的索引位置上取得或設定項目。Gets or sets the element at the specified index.

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

IList 移除特定物件之第一個符合的元素。Removes the first occurrence of a specific object from the IList.

IList.Remove(Object)

CollectionBase 移除特定物件之第一個符合的元素。Removes the first occurrence of a specific object from the CollectionBase.

(繼承來源 CollectionBase)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於