欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用SqlBulkCopy批量復制數(shù)據(jù)到數(shù)據(jù)表

 更新時間:2014年10月27日 11:03:15   投稿:shichen2014  
這篇文章主要介紹了C#使用SqlBulkCopy批量復制數(shù)據(jù)到數(shù)據(jù)表的方法,較為詳細的講述了SqlBulkCopy批量復制數(shù)據(jù)到數(shù)據(jù)表的原理與實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了C#使用SqlBulkCopy批量復制數(shù)據(jù)到數(shù)據(jù)表的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

使用 SqlBulkCopy 類只能向 SQL Server 表寫入數(shù)據(jù)。但是,數(shù)據(jù)源不限于 SQL Server;可以使用任何數(shù)據(jù)源,只要數(shù)據(jù)可加載到 DataTable 實例或可使用 IDataReader 實例讀取數(shù)據(jù)

1.使用Datatable作為數(shù)據(jù)源的方式:

下面的代碼使用到了ColumnMappings,因為目標表和數(shù)據(jù)源Datatable的結構不一致,需要這么一個映射來指定對應關系

復制代碼 代碼如下:
public string SaveJHCData(LzShopBasicData[] datas)
{
    var result = new AResult();
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["**"].ConnectionString);
    con.Open();
    foreach (var item in datas)
    {

 Logger.Info("數(shù)據(jù)更新處理,店鋪名稱:" + item.ShopName + "數(shù)據(jù)日期" + item.SellDate);
 try
 {
     using (TransactionScope scope = new TransactionScope())
     {

  DataTable JHCOrderItemsdt = SaveJHCOrderItemsData(item);
  SqlBulkCopy JHCOrderItemscopy = new SqlBulkCopy(con);
  JHCOrderItemscopy.ColumnMappings.Add("orderId", "orderId");
  JHCOrderItemscopy.ColumnMappings.Add("auctionId", "auctionId");
  JHCOrderItemscopy.ColumnMappings.Add("itemTitle", "itemTitle");
  JHCOrderItemscopy.ColumnMappings.Add("tradeAmt", "tradeAmt");
  JHCOrderItemscopy.ColumnMappings.Add("alipayNum", "alipayNum");
  JHCOrderItemscopy.ColumnMappings.Add("tradeTime", "tradeTime");
  JHCOrderItemscopy.ColumnMappings.Add("uv", "uv");
  JHCOrderItemscopy.ColumnMappings.Add("srcId", "srcId");
  JHCOrderItemscopy.ColumnMappings.Add("srcName", "srcName");
  JHCOrderItemscopy.ColumnMappings.Add("DataType", "DataType");
  JHCOrderItemscopy.ColumnMappings.Add("DataDate", "DataDate");
  JHCOrderItemscopy.ColumnMappings.Add("OrderSourceID", "OrderSourceID");
  JHCOrderItemscopy.ColumnMappings.Add("ShopName", "ShopName");
  JHCOrderItemscopy.DestinationTableName = "JHCOrderItems";
  JHCOrderItemscopy.WriteToServer(JHCOrderItemsdt);
  result.Updatedata += 1;
  result.UpdatedataText += item.SellDate + ",";
  scope.Complete();
  Logger.Info(item.SellDate + "事務提交");
     }
 }
 catch (Exception ex)
 {
     Logger.Error(ex.ToString());
     continue;
 }
    }
    con.Close();
    return result.ToSerializeObject();
}

2.使用IDataReader作為數(shù)據(jù)源的方式,這種方式個人認為用的很少,首先目標表和來源表兩個數(shù)據(jù)庫連接你都需要拿到,如果兩個都可以拿到,一般直接操作sql就可以解決:

這里是直接拷貝的MSDN的代碼,

用到的AdventureWorks數(shù)據(jù)庫可以直接在網(wǎng)上下載到,下載地址如下:http://msftdbprodsamples.codeplex.com/releases

復制代碼 代碼如下:
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;";
    }
}

實戰(zhàn):借助類型反射動態(tài)構建Datatable數(shù)據(jù)源,通過SqlBulkCopy批量保存入庫

1.獲取一張空的Datatable:

復制代碼 代碼如下:
var dt = bisdal.From<TopBrand>(TopBrand._.ID == -1, OrderByClip.Default).ToDataTable();

2.填充DataTable,這里是通過遍歷外部的集合,把屬性屬性逐一賦值填充到目標Datatable

復制代碼 代碼如下:
foreach (var item in brandselldataitems)
{
 try
 {

     TopBrand topbrand = new TopBrand
     {
  BrandIndex = item.mk,
  BrandName = item.c58,
  Date = date,
  WinnerAmt = item.c60,
  WinnerPeople = item.c62,
  WinnerProNum = item.c61,
  HotTaobaoCategoryID = cid
     };
     CreateDtByItem<TopBrand>(topbrand, dt);
 }
 catch (Exception ex)
 {
     Logger.Error(ex.ToString());
     continue;
 }
}

這里借助反射,遍歷實體屬性集合,動態(tài)構建DataTableRow對象

復制代碼 代碼如下:
private void CreateDtByItem<T>(T item, DataTable dt)
{
    System.Reflection.PropertyInfo[] properties = item.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    var newrow = dt.NewRow();
    foreach (System.Reflection.PropertyInfo pitem in properties)
    {

 string name = pitem.Name;
 if (name == "children")
 {
     continue;
 }
 object value = pitem.GetValue(item, null);
 newrow[name] = value == null ? DBNull.Value : value;
    }
    dt.Rows.Add(newrow);
}

3.保存入庫:

復制代碼 代碼如下:
BulkWriteToServer(con, "TopBrand", dt);

這里因為目標表和數(shù)據(jù)源的Datatable數(shù)據(jù)結構一致,所以省去了ColumnMappings列映射的操作,可以直接WriteToServer保存

復制代碼 代碼如下:
private void BulkWriteToServer(SqlConnection con, string destinationtablename, DataTable sourcedt)
{
    try
    {
 if (con.State == ConnectionState.Closed)
 {
     con.Open();
 }
 SqlBulkCopy topbranddtcopy = new SqlBulkCopy(con);
 topbranddtcopy.DestinationTableName = destinationtablename;
 topbranddtcopy.WriteToServer(sourcedt);
 con.Close();
    }
    catch (Exception ex)
    {
 Logger.Error("批量新增數(shù)據(jù):" + destinationtablename + "," + ex.ToString());
    }
}

完整調用代碼:

復制代碼 代碼如下:
private void CreateTopBrandData(int date, int cid, List<BrandSellDataItem> brandselldataitems)
{
    try
    {
 var dt = bisdal.From<TopBrand>(TopBrand._.ID == -1, OrderByClip.Default).ToDataTable();
 foreach (var item in brandselldataitems)
 {
     try
     {

  TopBrand topbrand = new TopBrand
  {
      BrandIndex = item.mk,
      BrandName = item.c58,
      Date = date,
      WinnerAmt = item.c60,
      WinnerPeople = item.c62,
      WinnerProNum = item.c61,
      HotTaobaoCategoryID = cid
  };
  CreateDtByItem<TopBrand>(topbrand, dt);
     }
     catch (Exception ex)
     {
  Logger.Error(ex.ToString());
  continue;
     }
 }
 BulkWriteToServer(con, "TopBrand", dt);
    }
    catch (Exception ex)
    {
 throw new Exception("CreateTopBrandData:" + ex.ToString());
    }
}

希望本文所述對大家的C#程序設計有所幫助。

相關文章

  • C#在RichTextBox中顯示不同顏色文字的方法

    C#在RichTextBox中顯示不同顏色文字的方法

    這篇文章主要介紹了C#在RichTextBox中顯示不同顏色文字的方法,實例分析了C#中RichTextBox控件控制文字顯示效果的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • 淺談C# 類的繼承

    淺談C# 類的繼承

    本文主要介紹了C# 類的繼承相關知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • C#實現(xiàn)多線程編程的簡單案例

    C#實現(xiàn)多線程編程的簡單案例

    這篇文章介紹了C#實現(xiàn)多線程編程的簡單案例,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • Unity3D實現(xiàn)待機狀態(tài)圖片循環(huán)淡入淡出

    Unity3D實現(xiàn)待機狀態(tài)圖片循環(huán)淡入淡出

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)待機狀態(tài)圖片循環(huán)淡入淡出,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#實現(xiàn)語音播報功能的示例詳解

    C#實現(xiàn)語音播報功能的示例詳解

    這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)語音播報功能,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2024-02-02
  • C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法

    C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法

    這篇文章主要介紹了C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法,涉及C#針對矩陣的數(shù)學運算相關實現(xiàn)技巧,需要的朋友可以參考下
    2015-08-08
  • C#用遞歸算法解決經(jīng)典背包問題

    C#用遞歸算法解決經(jīng)典背包問題

    背包問題有好多版本,本文只研究0/1版本,即對一個物體要么選用,要么就拋棄,不能將一個物體再繼續(xù)細分的情況。
    2016-06-06
  • C#將布爾類型轉換成字節(jié)數(shù)組的方法

    C#將布爾類型轉換成字節(jié)數(shù)組的方法

    這篇文章主要介紹了C#將布爾類型轉換成字節(jié)數(shù)組的方法,涉及C#中字符串函數(shù)的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • C#如何實現(xiàn)圖片查看器

    C#如何實現(xiàn)圖片查看器

    這篇文章主要為大家詳細介紹了C#如何實現(xiàn)圖片查看器的相關方法,C#實現(xiàn)一個像Windows自帶的圖片查看器的功能,感興趣的小伙伴們可以參考一下
    2016-04-04
  • C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別

    C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別

    這篇文章主要給大家介紹了關于C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09

最新評論