你的位置:首页 > 软件开发 > ASP.net > .NET批量大数据插入性能分析及比较

.NET批量大数据插入性能分析及比较

发布时间:2015-04-14 14:00:10
数据插入使用了以下几种方式1. 逐条数据插入2. 拼接sql语句批量插入3. 拼接sql语句并使用Transaction4. 拼接sql语句并使用SqlTransaction5. 使用DataAdapter6. 使用TransactionScope及SqlBulkCopy7. 使 ...

.NET批量大数据插入性能分析及比较

 

数据插入使用了以下几种方式

1. 逐条数据插入

  1. public delegate bool InsertHandler(DataTable table, int batchSize);  
  2.   
  3. public partial class FrmBatch : Form  
  4. {  
  5.     private Stopwatch _watch = new Stopwatch();  
  6.   
  7.     public FrmBatch()  
  8.     {  
  9.         InitializeComponent();  
  10.     }  
  11.   
  12.     private void FrmBatch_Load(object sender, EventArgs e)  
  13.     {  
  14.         txtRecordCount.Text = "40000";  
  15.         txtBatchSize.Text = "1";  
  16.     }  
  17.   
  18.     //逐条数据插入   
  19.     private void btnInsert_Click(object sender, EventArgs e)  
  20.     {  
  21.         Insert(DbOperation.ExecuteInsert, "Use SqlServer Insert");  
  22.     }  
  23.   
  24.     //拼接sql语句插入   
  25.     private void btnBatchInsert_Click(object sender, EventArgs e)  
  26.     {  
  27.         Insert(DbOperation.ExecuteBatchInsert, "Use SqlServer Batch Insert");  
  28.     }  
  29.   
  30.     //拼接sql语句并使用Transaction   
  31.     private void btnTransactionInsert_Click(object sender, EventArgs e)  
  32.     {  
  33.         Insert(DbOperation.ExecuteTransactionInsert, "Use SqlServer Batch Transaction Insert");  
  34.     }  
  35.   
  36.     //拼接sql语句并使用SqlTransaction   
  37.     private void btnSqlTransactionInsert_Click(object sender, EventArgs e)  
  38.     {  
  39.         Insert(DbOperation.ExecuteSqlTransactionInsert, "Use SqlServer Batch SqlTransaction Insert");  
  40.     }  
  41.   
  42.     //使用DataAdapter   
  43.     private void btnDataAdapterInsert_Click(object sender, EventArgs e)  
  44.     {  
  45.         Insert(DbOperation.ExecuteDataAdapterInsert, "Use SqlServer DataAdapter Insert");  
  46.     }  
  47.   
  48.     //使用TransactionScope   
  49.     private void btnTransactionScopeInsert_Click(object sender, EventArgs e)  
  50.     {  
  51.         Insert(DbOperation.ExecuteTransactionScopeInsert, "Use SqlServer TransactionScope Insert");  
  52.     }  
  53.   
  54.     //使用表值参数   
  55.     private void btnTableTypeInsert_Click(object sender, EventArgs e)  
  56.     {  
  57.         Insert(DbOperation.ExecuteTableTypeInsert, "Use SqlServer TableType Insert");  
  58.     }  
  59.   
  60.     private DataTable InitDataTable()  
  61.     {  
  62.         DataTable table = Tools.MakeDataTable();  
  63.         int count = 0;  
  64.         if (int.TryParse(txtRecordCount.Text.Trim(), out count))  
  65.         {  
  66.             Tools.MakeData(table, count);  
  67.             //MessageBox.Show("Data Init OK");   
  68.         }  
  69.         return table;  
  70.     }  
  71.   
  72.     public void Insert(InsertHandler handler, get='_blank'>string msg)  
  73.     {  
  74.         DataTable table = InitDataTable();  
  75.         if (table == null)  
  76.         {  
  77.             MessageBox.Show("DataTable is null");  
  78.             return;  
  79.         }  
  80.   
  81.         int recordCount = table.Rows.Count;  
  82.         if (recordCount <= 0)  
  83.         {  
  84.             MessageBox.Show("No Data");  
  85.             return;  
  86.         }  
  87.   
  88.         int batchSize = 0;  
  89.         int.TryParse(txtBatchSize.Text.Trim(), out batchSize);  
  90.         if (batchSize <= 0)  
  91.         {  
  92.             MessageBox.Show("batchSize <= 0");  
  93.             return;  
  94.         }  
  95.   
  96.         bool result = false;  
  97.         _watch.Reset(); _watch.Start();  
  98.         result = handler(table, batchSize);  
  99.         _watch.Stop(www.nuoya66.com);  
  100.         string log = string.Format("{0};RecordCount:{1};BatchSize:{2};Time:{3};", msg, recordCount, batchSize, _watch.ElapsedMilliseconds);  
  101.         LogHelper.Info(log);  
  102.         MessageBox.Show(result.ToString());  
  103.     }  
  104. }  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:.NET批量大数据插入性能分析及比较

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。