A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© aisini 金牌黑马   /  2014-8-13 14:25  /  653 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Data;
  6. using System.Configuration;
  7. using System.Data.SqlClient;

  8. namespace DemoApp
  9. {

  10. public class MyRecord
  11. {
  12.     public int TestInt;
  13.     public string TestString;

  14.     public MyRecord()
  15.     {
  16.     }

  17.     public MyRecord(int myInt, string myString)
  18.     {
  19.         this.TestInt = myInt;
  20.         this.TestString = myString;
  21.     }
  22. }

  23. public class BulkUploadToSql
  24. {
  25.     private List<MyRecord> internalStore;

  26.     protected string tableName;
  27.     protected DataTable dataTable = new DataTable();
  28.     protected int recordCount;
  29.     protected int commitBatchSize;
  30.     private BulkUploadToSql(
  31.         string tableName,
  32.         int commitBatchSize)
  33.     {
  34.         internalStore = new List<MyRecord>();

  35.         this.tableName = tableName;
  36.         dataTable = new DataTable(tableName);
  37.         recordCount = 0;
  38.         commitBatchSize = commitBatchSize;

  39.         // add columns to this data table
  40.         InitializeStructures();
  41.     }

  42.     private BulkUploadToSql() :
  43.         this("MyTableName", 1000) {}

  44. private void InitializeStructures()
  45. {
  46.     dataTable.Columns.Add("TI", typeof(Int32));
  47.     dataTable.Columns.Add("TS", typeof(string));
  48. }

  49. public static BulkUploadToSql Load(Stream dataSource)
  50. {
  51.     // create a new object to return
  52.     BulkUploadToSql o = new BulkUploadToSql();
  53.      
  54.     // replace the code below
  55.     // with your custom logic
  56.     for (int cnt = 0; cnt < 10000; cnt++)
  57.     {
  58.         MyRecord rec =
  59.             new MyRecord
  60.             (
  61.             cnt,
  62.             string.Format("string{0}", cnt)
  63.             );
  64.         o.internalStore.Add(rec);
  65.     }

  66.     return o;
  67. }

  68. private void WriteToDatabase()
  69. {
  70.     // get your connection string
  71.     string connString = "";
  72.     // connect to SQL
  73.     using (SqlConnection connection =
  74.             new SqlConnection(connString))
  75.     {
  76.         // make sure to enable triggers
  77.         // more on triggers in next post
  78.         SqlBulkCopy bulkCopy =
  79.             new SqlBulkCopy
  80.             (
  81.             connection,
  82.             SqlBulkCopyOptions.TableLock |
  83.             SqlBulkCopyOptions.FireTriggers |
  84.             SqlBulkCopyOptions.UseInternalTransaction,
  85.             null
  86.             );

  87.         // set the destination table name
  88.         bulkCopy.DestinationTableName = this.tableName;
  89.         connection.Open();

  90.         // write the data in the "dataTable"
  91.         bulkCopy.WriteToServer(dataTable);
  92.         connection.Close();
  93.     }
  94.     // reset
  95.     dataTable.Clear();
  96.     recordCount = 0;
  97. }

  98. public void Flush()
  99. {
  100.     // transfer data to the datatable
  101.     foreach (MyRecord rec in this.internalStore)
  102.     {
  103.         PopulateDataTable(rec);
  104.         if (recordCount >= this.commitBatchSize)
  105.             WriteToDatabase();
  106.     }
  107.     // write remaining records to the DB
  108.     if (recordCount > 0)
  109.         WriteToDatabase();
  110. }

  111. private void PopulateDataTable(MyRecord record)
  112. {
  113.     // populate the values
  114.     // using your custom logic
  115.     DataRow row = this.dataTable.NewRow();

  116.     row[0] = record.TestInt;
  117.     row[1] = record.TestString;

  118.     // add it to the base for final addition to the DB
  119.     dataTable.Rows.Add(row);
  120.     recordCount++;
  121. }
  122. }

  123. class Program
  124. {
  125.     static void Main(string[] args)
  126.     {
  127.         using (Stream s =
  128.             new StreamReader(@"C:\TestData.txt"))
  129.         {
  130.             BulkUploadToSql myData =
  131.                 BulkUploadToSql.Load(s);
  132.             myData.Flush();
  133.         }
  134.     }
  135. }
  136. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马