using System.Data.SqlClient;
using System;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
SqlCommand commandSourceData = new SqlCommand(
"SELECT * FROM BulkCopyDemoMatchingColumns;", sourceConnection);//源
SqlDataReader reader =
commandSourceData.ExecuteReader();
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoDifferentColumns";//目标
//bulkCopy.ColumnMappings.Add("源","目标");
bulkCopy.ColumnMappings.Add("ProductID","ProdID");
bulkCopy.ColumnMappings.Add("Name","ProdName");
bulkCopy.ColumnMappings.Add("ProductNumber","ProdNum");
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}
}
}
private static string GetConnectionString()
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
如果数据量不是巨大,几乎是瞬间完成的!比较常用于Excel和SQL之间的数据导入!
转载于:https://www.cnblogs.com/SUPERAI/archive/2011/11/28/2266752.html