.NET批量大数据插入性能分析及比较
发布时间:2015-04-14 14:00:10
数据插入使用了以下几种方式1. 逐条数据插入2. 拼接sql语句批量插入3. 拼接sql语句并使用Transaction4. 拼接sql语句并使用SqlTransaction5. 使用DataAdapter6. 使用TransactionScope及SqlBulkCopy7. 使 ...
数据插入使用了以下几种方式
1. 逐条数据插入
- public delegate bool InsertHandler(DataTable table, int batchSize);
-
- public partial class FrmBatch : Form
- {
- private Stopwatch _watch = new Stopwatch();
-
- public FrmBatch()
- {
- InitializeComponent();
- }
-
- private void FrmBatch_Load(object sender, EventArgs e)
- {
- txtRecordCount.Text = "40000";
- txtBatchSize.Text = "1";
- }
-
- //逐条数据插入
- private void btnInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteInsert, "Use SqlServer Insert");
- }
-
- //拼接sql语句插入
- private void btnBatchInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteBatchInsert, "Use SqlServer Batch Insert");
- }
-
- //拼接sql语句并使用Transaction
- private void btnTransactionInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteTransactionInsert, "Use SqlServer Batch Transaction Insert");
- }
-
- //拼接sql语句并使用SqlTransaction
- private void btnSqlTransactionInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteSqlTransactionInsert, "Use SqlServer Batch SqlTransaction Insert");
- }
-
- //使用DataAdapter
- private void btnDataAdapterInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteDataAdapterInsert, "Use SqlServer DataAdapter Insert");
- }
-
- //使用TransactionScope
- private void btnTransactionScopeInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteTransactionScopeInsert, "Use SqlServer TransactionScope Insert");
- }
-
- //使用表值参数
- private void btnTableTypeInsert_Click(object sender, EventArgs e)
- {
- Insert(DbOperation.ExecuteTableTypeInsert, "Use SqlServer TableType Insert");
- }
-
- private DataTable InitDataTable()
- {
- DataTable table = Tools.MakeDataTable();
- int count = 0;
- if (int.TryParse(txtRecordCount.Text.Trim(), out count))
- {
- Tools.MakeData(table, count);
- //MessageBox.Show("Data Init OK");
- }
- return table;
- }
-
- public void Insert(InsertHandler handler, get='_blank'>string msg)
- {
- DataTable table = InitDataTable();
- if (table == null)
- {
- MessageBox.Show("DataTable is null");
- return;
- }
-
- int recordCount = table.Rows.Count;
- if (recordCount <= 0)
- {
- MessageBox.Show("No Data");
- return;
- }
-
- int batchSize = 0;
- int.TryParse(txtBatchSize.Text.Trim(), out batchSize);
- if (batchSize <= 0)
- {
- MessageBox.Show("batchSize <= 0");
- return;
- }
-
- bool result = false;
- _watch.Reset(); _watch.Start();
- result = handler(table, batchSize);
- _watch.Stop(www.nuoya66.com);
- string log = string.Format("{0};RecordCount:{1};BatchSize:{2};Time:{3};", msg, recordCount, batchSize, _watch.ElapsedMilliseconds);
- LogHelper.Info(log);
- MessageBox.Show(result.ToString());
- }
- }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:.NET批量大数据插入性能分析及比较
关键词:.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。