数据源最快导入方法
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之间的数据导入!
文章作者:
蔚洋(Super)
文章出处:
http://www.cnblogs.com/SUPERAI
欢迎转载,转载时请注明出处。谢谢合作。