zoukankan      html  css  js  c++  java
  • .NET 海量数据处理,并处理事务问题

    1.下面是一个C#的控制台以代码来说明处理

    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

  • 相关阅读:
    all the tops
    es6 and typescript
    [leetcode]question5: Longest Palindromic Substring
    webpack and publish lib
    HTTPClient to use http/https protocol to send request
    《算法导论》-分治法-笔记
    《Linux C编程一站式学习》-笔记
    WIN7中同时打开多个独立Excel窗口
    RAD Studio XE6之Tpanel
    vb中StatusBar1.Panels(3).Text = Format(Date, "yyyy年mm月dd日")是什么意思
  • 原文地址:https://www.cnblogs.com/hornet/p/4777682.html
Copyright © 2011-2022 走看看