批量添加数据SqlBulkCopy
2024-09-28 20:03:03
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;";
}
}
程序员的基础教程:菜鸟程序员
最新文章
- mysql 主从复制实现步骤
- 关于checkbox全选与反选的问题
- Mathout 安装部署
- dedecms发布文章时多个Tag间分割逗号自动变成英文逗号
- 夺命雷公狗---微信开发56----微信js-sdk接口开发(3)所有接口功能
- js 正则表达式 查找
- Oracle PL/SQL 游标
- 编程算法 - 二叉树的深度 代码(C)
- CSLA .NET是一个.NET软件开发框架
- JavaScript基础学习(七)—BOM
- 译-BSA NSH Command介绍
- Gin框架初识
- 不修改模板的前提下修改VisualState中的某些值
- Linux命令——压缩和解压缩
- C#.NET常见问题(FAQ)-如何让文本框textbox内容限制为数字
- 给Jquery easyui 的datagrid 每行增加操作链接(转)
- HDUOJ-------Naive and Silly Muggles
- Pandas -- Merge,join and concatenate
- LeetCode总结【转】
- 拒绝干扰 解决Wi-Fi的最大问题
热门文章
- Java基础总结大全
- Bootstrap-table学习笔记(二)——前后端分页模糊查询
- c#和c++互操作(平台调用相关)
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
- PADS Logic 脚本的 Fields 一个对象记录
- CENTOS7安装DOCKER步骤以及安装阿里镜像加速后无法正常启动服务的问题2018年1月
- <;trim>;: prefix+prefixOverrides+suffix+suffixOverrides
- str_split的用法(PHP学习)
- SpringMVC上传文件大小的设置
- 小程序App方法