- ·上一篇文章:最佳实践 ADO.NET实用经验无保留曝光(1)
- ·下一篇文章:NHibernate与Ado.Net查询速度的比较(1)
NHibernate与Ado.Net查询速度的比较(2)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NHibernateTestUI
{
public partial class OrderFrm : Form
{
public OrderFrm()
{
InitializeComponent();
}
NHibernateTest.Order order = new NHibernateTest.Order();
/**//// <summary>
/// 使用NHibernate HQL方式查询
/// </summary>
private void btnNHLoad_Click( object sender, EventArgs e )
{
string strOutput = "";
// 测试加载数据时间
DateTime dtStart = System.DateTime.Now;
IList orders = order.LoadAllByNHibernate(); // 使用NHibernate HQL方式查询
DateTime dtEnd = System.DateTime.Now;
TimeSpan ts = dtEnd - dtStart;
strOutput += "NHibernate HQL方式 : "
+ orders.Count + " 条记录, 加载时间(ms) : "
+ ts.TotalMilliseconds;
// 测试绑定数据时间
dtStart = System.DateTime.Now;
this.dgvOrders.DataSource = orders;
dtEnd = System.DateTime.Now;
ts = dtEnd - dtStart;
strOutput += " 绑定IList时间 : " + ts.TotalMilliseconds;
Console.WriteLine( strOutput );
}
/**//// <summary>
/// 使用Ado.Net DbDataAdapter方式查询
/// </summary>
private void btnADOLoad_Click( object sender, EventArgs e )
{
string strOutput = "";
// 测试加载数据时间
DateTime dtStart = System.DateTime.Now;
DataTable dtOrders = order.LoadAllByAdoNet(); // 使用Ado.Net DbDataAdapter方式查询
DateTime dtEnd = System.DateTime.Now;
TimeSpan ts = dtEnd - dtStart;
strOutput += "Ado.Net DbDataAdapter方式 : "
+ dtOrders.Rows.Count + " 条记录; 加载时间(ms) : "
+ ts.TotalMilliseconds;
// 测试绑定数据时间
dtStart = System.DateTime.Now;
this.dgvOrders.DataSource = dtOrders;
dtEnd = System.DateTime.Now;
ts = dtEnd - dtStart;
strOutput += " 绑定IList时间 : " + ts.TotalMilliseconds;
Console.WriteLine( strOutput );
}
}
}
运行后界面如下:

在界面两个按钮点击多次后,两者的查询时间稳定,可以作为比较。下面是输出:
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952 绑定IList时间 : 70.1008
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3304.752 绑定IList时间 : 70.1008
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3354.824 绑定IList时间 : 80.1152
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576 绑定IList时间 : 80.1152
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576 绑定IList时间 : 90.1296
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 30.0432 绑定IList时间 : 90.1296
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952 绑定IList时间 : 70.1008
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 50.072 绑定IList时间 : 90.1296
另外在网上找了一下关于
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NHibernateTestUI
{
public partial class OrderFrm : Form
{
public OrderFrm()
{
InitializeComponent();
}
NHibernateTest.Order order = new NHibernateTest.Order();
/**//// <summary>
/// 使用NHibernate HQL方式查询
/// </summary>
private void btnNHLoad_Click( object sender, EventArgs e )
{
string strOutput = "";
// 测试加载数据时间
DateTime dtStart = System.DateTime.Now;
IList orders = order.LoadAllByNHibernate(); // 使用NHibernate HQL方式查询
DateTime dtEnd = System.DateTime.Now;
TimeSpan ts = dtEnd - dtStart;
strOutput += "NHibernate HQL方式 : "
+ orders.Count + " 条记录, 加载时间(ms) : "
+ ts.TotalMilliseconds;
// 测试绑定数据时间
dtStart = System.DateTime.Now;
this.dgvOrders.DataSource = orders;
dtEnd = System.DateTime.Now;
ts = dtEnd - dtStart;
strOutput += " 绑定IList时间 : " + ts.TotalMilliseconds;
Console.WriteLine( strOutput );
}
/**//// <summary>
/// 使用Ado.Net DbDataAdapter方式查询
/// </summary>
private void btnADOLoad_Click( object sender, EventArgs e )
{
string strOutput = "";
// 测试加载数据时间
DateTime dtStart = System.DateTime.Now;
DataTable dtOrders = order.LoadAllByAdoNet(); // 使用Ado.Net DbDataAdapter方式查询
DateTime dtEnd = System.DateTime.Now;
TimeSpan ts = dtEnd - dtStart;
strOutput += "Ado.Net DbDataAdapter方式 : "
+ dtOrders.Rows.Count + " 条记录; 加载时间(ms) : "
+ ts.TotalMilliseconds;
// 测试绑定数据时间
dtStart = System.DateTime.Now;
this.dgvOrders.DataSource = dtOrders;
dtEnd = System.DateTime.Now;
ts = dtEnd - dtStart;
strOutput += " 绑定IList时间 : " + ts.TotalMilliseconds;
Console.WriteLine( strOutput );
}
}
}
运行后界面如下:
在界面两个按钮点击多次后,两者的查询时间稳定,可以作为比较。下面是输出:
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952 绑定IList时间 : 70.1008
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3304.752 绑定IList时间 : 70.1008
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3354.824 绑定IList时间 : 80.1152
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576 绑定IList时间 : 80.1152
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576 绑定IList时间 : 90.1296
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 30.0432 绑定IList时间 : 90.1296
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952 绑定IList时间 : 70.1008
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 50.072 绑定IList时间 : 90.1296
另外在网上找了一下关于
