SqlBulkCopyColumnMapping.DestinationOrdinal 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
目标表中目标列的序号值。
public:
property int DestinationOrdinal { int get(); void set(int value); };
public int DestinationOrdinal { get; set; }
member this.DestinationOrdinal : int with get, set
Public Property DestinationOrdinal As Integer
属性值
DestinationOrdinal 属性的整数值,或者如果尚未设置该属性,则为 -1。
示例
以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名称和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于为大容量复制创建列映射。
重要
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。
提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一SQL Server实例中,则使用 Transact-SQL INSERT … SELECT
语句复制数据会更轻松、更快。
using Microsoft.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";
// Set up the column mappings source and destination.
SqlBulkCopyColumnMapping mapID = new SqlBulkCopyColumnMapping();
mapID.SourceOrdinal = 0;
mapID.DestinationOrdinal = 0;
bulkCopy.ColumnMappings.Add(mapID);
SqlBulkCopyColumnMapping mapName = new SqlBulkCopyColumnMapping();
mapName.SourceOrdinal = 1;
mapName.DestinationOrdinal = 2;
bulkCopy.ColumnMappings.Add(mapName);
SqlBulkCopyColumnMapping mapNumber = new SqlBulkCopyColumnMapping();
mapNumber.SourceOrdinal = 2;
mapNumber.DestinationOrdinal = 1;
bulkCopy.ColumnMappings.Add(mapNumber);
// 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;";
}
}
注解
和 DestinationColumnDestinationOrdinal 属性互斥。 最后一个设置的值优先。