SqlBulkCopyColumnMappingCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
繼承自 CollectionBase 的 SqlBulkCopyColumnMapping 物件集合。
public ref class SqlBulkCopyColumnMappingCollection sealed : System::Collections::IList
public ref class SqlBulkCopyColumnMappingCollection sealed : System::Collections::CollectionBase
public sealed class SqlBulkCopyColumnMappingCollection : System.Collections.IList
public sealed class SqlBulkCopyColumnMappingCollection : System.Collections.CollectionBase
type SqlBulkCopyColumnMappingCollection = class
interface ICollection
interface IEnumerable
interface IList
type SqlBulkCopyColumnMappingCollection = class
inherit CollectionBase
Public NotInheritable Class SqlBulkCopyColumnMappingCollection
Implements IList
Public NotInheritable Class SqlBulkCopyColumnMappingCollection
Inherits CollectionBase
- 繼承
-
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) |
方法
明確介面實作
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
適用於
另請參閱
- 在 SQL Server 中執行大量複製作業
- ADO.NET 概觀 \(部分機器翻譯\)