.NET批量大数据插入性能分析及比较
作者:网络转载 发布时间:[ 2015/4/15 11:00:11 ] 推荐标签:.NET 性能分析
使用Log4net记录日志,默认插入记录数为40000条,每次插入1条,可在界面修改,使用System.Diagnostics.StopWatch记录插入时间,每次测试后删除原表重建
窗体代码如下:
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, 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());
}
}
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
编程常用的几种时间戳转换(java .net 数据库).Net中关于相等的问题Asp.net MVC如何对所有用户输入的字符串字段做Trim处理Asp.Net WebForm生命周期的详解.Net开发的两个小技巧asp.net 六大内置对象.Net基础体系和跨框架开发普及Linux使用Jexus托管Asp.Net Core应用程序asp.net登录验证码实现方法ASP.NET自带对象JSON字符串与实体类的转换从 .NET 和 Java 之争谈 IT 行业.Net高效开发之不可错过的实用工具ASP.NET MVC必须知道的那些事!.NET中使用无闪刷新控件时提示框不显示.net开发中要注意的事项Asp.net Core MVC中使用Session

sales@spasvo.com