SqlBulkCopyColumnMappingCollection 类

定义

CollectionBase 继承的 SqlBulkCopyColumnMapping 对象的集合。

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”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名称和序号位置不匹配。 SqlBulkCopyColumnMapping将 添加到 对象的 中SqlBulkCopyColumnMappingCollectionSqlBulkCopy,以便为大容量复制创建列映射。

重要

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。 提供此代码是为了演示仅使用 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如果集合不为空,则不必指定数据源中存在的每一列。 未由集合映射的那些将被忽略。

可以按名称或序号引用源列和目标列。 可以在同一映射集合中混合按名称和按序号列引用。

属性

Capacity

获取或设置 CollectionBase 可包含的元素数。

(继承自 CollectionBase)
Count

获取 SqlBulkCopyColumnMappingCollection 中包含的元素数。

Count

获取 CollectionBase 实例中包含的元素数。 不能重写此属性。

(继承自 CollectionBase)
InnerList

获取一个 ArrayList,它包含 CollectionBase 实例中元素的列表。

(继承自 CollectionBase)
Item[Int32]

获取位于指定索引位置的 SqlBulkCopyColumnMapping 对象。

List

获取一个 IList,它包含 CollectionBase 实例中元素的列表。

(继承自 CollectionBase)

方法

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()

返回循环访问集合的枚举数。

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)

显式接口实现

ICollection.CopyTo(Array, Int32)

从特定的 ICollection 索引开始,将 Array 的元素复制到一个 Array 中。

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 CollectionBase 复制到兼容的一维 Array

(继承自 CollectionBase)
ICollection.IsSynchronized

获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。

ICollection.IsSynchronized

获取一个值,该值指示是否同步对 CollectionBase 的访问(线程安全)。

(继承自 CollectionBase)
ICollection.SyncRoot

获取可用于同步对 ICollection 的访问的对象。

ICollection.SyncRoot

获取可用于同步对 CollectionBase 的访问的对象。

(继承自 CollectionBase)
IList.Add(Object)

将某项添加到 IList 中。

IList.Add(Object)

将对象添加到 CollectionBase 的结尾处。

(继承自 CollectionBase)
IList.Contains(Object)

确定 IList 是否包含特定值。

IList.Contains(Object)

确定 CollectionBase 是否包含特定元素。

(继承自 CollectionBase)
IList.IndexOf(Object)

确定 IList 中特定项的索引。

IList.IndexOf(Object)

搜索指定的 Object,并返回整个 CollectionBase 中第一个匹配项的从零开始的索引。

(继承自 CollectionBase)
IList.Insert(Int32, Object)

IList 中的指定索引处插入一个项。

IList.Insert(Int32, Object)

将元素插入 CollectionBase 的指定索引处。

(继承自 CollectionBase)
IList.IsFixedSize

获取一个值,该值指示 IList 是否具有固定大小。

IList.IsFixedSize

获取一个值,该值指示 CollectionBase 是否具有固定大小。

(继承自 CollectionBase)
IList.IsReadOnly

获取一个值,该值指示 IList 是否为只读。

IList.IsReadOnly

获取一个值,该值指示 CollectionBase 是否为只读。

(继承自 CollectionBase)
IList.Item[Int32]

获取或设置指定索引处的元素。

IList.Item[Int32]

获取或设置指定索引处的元素。

(继承自 CollectionBase)
IList.Remove(Object)

IList 中移除特定对象的第一个匹配项。

IList.Remove(Object)

CollectionBase 中移除特定对象的第一个匹配项。

(继承自 CollectionBase)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

另请参阅