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.BulkCopyDemoMatchingColumns;",
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(); // Open the destination connection. In the real world you would
// not use SqlBulkCopy to move data from one table to the other
// in the same database. This is for demonstration purposes only.
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open(); // Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns"; try
{
// Write from the source to the destination.
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;";
}
}

程序员的基础教程:菜鸟程序员

最新文章

  1. mysql 主从复制实现步骤
  2. 关于checkbox全选与反选的问题
  3. Mathout 安装部署
  4. dedecms发布文章时多个Tag间分割逗号自动变成英文逗号
  5. 夺命雷公狗---微信开发56----微信js-sdk接口开发(3)所有接口功能
  6. js 正则表达式 查找
  7. Oracle PL/SQL 游标
  8. 编程算法 - 二叉树的深度 代码(C)
  9. CSLA .NET是一个.NET软件开发框架
  10. JavaScript基础学习(七)—BOM
  11. 译-BSA NSH Command介绍
  12. Gin框架初识
  13. 不修改模板的前提下修改VisualState中的某些值
  14. Linux命令——压缩和解压缩
  15. C#.NET常见问题(FAQ)-如何让文本框textbox内容限制为数字
  16. 给Jquery easyui 的datagrid 每行增加操作链接(转)
  17. HDUOJ-------Naive and Silly Muggles
  18. Pandas -- Merge,join and concatenate
  19. LeetCode总结【转】
  20. 拒绝干扰 解决Wi-Fi的最大问题

热门文章

  1. Java基础总结大全
  2. Bootstrap-table学习笔记(二)——前后端分页模糊查询
  3. c#和c++互操作(平台调用相关)
  4. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
  5. PADS Logic 脚本的 Fields 一个对象记录
  6. CENTOS7安装DOCKER步骤以及安装阿里镜像加速后无法正常启动服务的问题2018年1月
  7. <trim>: prefix+prefixOverrides+suffix+suffixOverrides
  8. str_split的用法(PHP学习)
  9. SpringMVC上传文件大小的设置
  10. 小程序App方法