zoukankan      html  css  js  c++  java
  • System.Data.SqlClient.SqlBulkCopy的使用方法

    C#
    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;";
    }
    }
    System.Object
      System.Data.SqlClient.SqlBulkCopy
  • 相关阅读:
    为什么说2013是PHP年
    wordpress 投稿插件 支持图片上传
    php简易页面内调试技巧
    WordPress中文文档
    百度网盘文件直链
    HOWTO:如何解决安装包在系统“添加/删除”中无法修复或卸载的问题
    InstallShield 2008 终止声明 (EOL)对最终客户意味着什么
    InstallShield 2011新功能试用(10) Express版本
    AdminStudio 9.5 Service Pack 3
    INFO:InstallShield中安装路径变量的区别
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/1422430.html
Copyright © 2011-2022 走看看