.NET 海量数据处理,并处理事务问题
2015-09-02 09:30
295 查看
1.下面是一个C#的控制台以代码来说明处理
参见MSDN:https://msdn.microsoft.com/en-us/library/tchktcdk(v=vs.110).aspx
using System.Data.SqlClient; class Program { static void Main() { string connectionString = GetConnectionString();
using (SqlConnection sourceConnection = new SqlConnection(connectionString)) { sourceConnection.Open(); // Delete all from the destination table. SqlCommand commandDelete = new SqlCommand(); commandDelete.Connection = sourceConnection; commandDelete.CommandText = "DELETE FROM dbo.BulkCopyDemoMatchingColumns"; commandDelete.ExecuteNonQuery(); SqlCommand commandInsert = new SqlCommand(); commandInsert.Connection = sourceConnection; commandInsert.CommandText = "SET IDENTITY_INSERT dbo.BulkCopyDemoMatchingColumns ON;" + "INSERT INTO " + "dbo.BulkCopyDemoMatchingColumns " + "([ProductID], [Name] ,[ProductNumber]) " + "VALUES(446, 'Lock Nut 23','LN-3416');" + "SET IDENTITY_INSERT dbo.BulkCopyDemoMatchingColumns OFF"; commandInsert.ExecuteNonQuery(); // 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(); // Set up the bulk copy object using the KeepIdentity option. using (SqlBulkCopy bulkCopy = new SqlBulkCopy( connectionString, SqlBulkCopyOptions.KeepIdentity)) { bulkCopy.BatchSize = 10; bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"; // Write from the source to the destination. // This should fail with a duplicate key error // after some of the batches have been copied. try { bulkCopy.WriteToServer(reader); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { 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;"; } }
参见MSDN:https://msdn.microsoft.com/en-us/library/tchktcdk(v=vs.110).aspx
相关文章推荐
- 谷歌换新LOGO了 适应更多智能终端
- PHP用正则表达式(re)验证邮箱
- 人脸识别核心算法
- iOS截屏
- 马云意外泄露最有效率的赚钱方法 惊呆百万人
- 关于STL扩展库的思考
- windows 10 中为开始菜单中的应用程序增加“以其他用户身份运行”菜单
- Android 自定义控件 轻松实现360软件详情页
- js实现获取当前时间的前几天日期
- 搭建简单的OSGI的hello world
- Oracle分页函数(存储过程)
- c# 防止重复运行 弹出已运行窗口并传递消息
- axis2 weibservice 入门
- eclipse插件卸载
- Point Cloud Library 1.7.2编译及使用详细教程
- 关闭弹窗页面后,刷新主页面
- Fedora10安装与配置Samba
- 铝电解电容总结[转]
- 在 Linux 下使用 RAID(七):在 RAID 中扩展现有的 RAID 阵列和删除故障的磁盘
- thinkphp全站静态页实现方法