SqlBulkCopyColumnMappingCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
繼承自 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
- 繼承
範例
下列範例會從 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 |
|---|---|
| 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 的專案。 |