C#中优化数据查询速度
来源:互联网 发布:淘宝看价格走势软件 编辑:程序博客网 时间:2024/05/21 16:58
方案一:采用DataSet作为数据源:
- 思路1:将List转换为DataSet,然后用DataSet作为数据源。
转换函数如下:
publicstatic DataSet ToDataSet(IList p_List)
{
DataSet result = new DataSet();
DataTable _DataTable = newDataTable();
if (p_List.Count > 0)
{
PropertyInfo[] propertys =p_List[0].GetType().GetProperties();
foreach (PropertyInfo pi inpropertys)
{
//_DataTable.Columns.Add(pi.Name, pi.PropertyType);
_DataTable.Columns.Add(pi.Name);
}
for (int i = 0; i <p_List.Count; i++)
{
ArrayList tempList = newArrayList();
foreach (PropertyInfo pi inpropertys)
{
object obj =pi.GetValue(p_List[i], null);
tempList.Add(obj);
}
object[] array =tempList.ToArray();
_DataTable.LoadDataRow(array, true);
}
}
result.Tables.Add(_DataTable);
return result;
}
数据绑定部分代码如下:
privatevoid RefreshData()
{
try
{
FormFactory.ShowWaitForm(this);
List<VisitorRegisterModel> list =VisitorRegisterBll.GetListDuringTime(dateEditStartTime.DateTime,dateEditEndTime.DateTime);
//this.gridControlVisitorRecord.DataSource = list;
DataSet ds = ToDataSet(list);
this.gridControlVisitorRecord.DataSource = ds;
this.gridViewVisitorRecord.BestFitColumns();
FormFactory.CloseWaitForm();
//判断是否有数据
if (list.Count <= 0)
{
FormFactory.ShowMessageBoxWarningDialog(this, "无数据!");
return;
}
}
catch (Exception ex)
{
FormFactory.ShowMessageBoxErrorDialog(this, "加载数据失败!");
GlobalData.Instance.Log.Error(string.Format("加载数据失败!{0}", ex.Message));
}
}
存在的问题:
如果直接将DataSet绑定到GridControl中虽然速度会加快但是会出现有个小+号的现象。如果直接用DataSet.Tables[0]作为数据源,则绑定速度又会慢下来。
- 思路二:直接重新建立数据库连接,从数据库直接取数据放入DataSet作为数据源。
privatevoid RefreshData()
{
try
{
FormFactory.ShowWaitForm(this);
//List<VisitorRegisterModel> list =VisitorRegisterBll.GetListDuringTime(dateEditStartTime.DateTime,dateEditEndTime.DateTime);
//this.gridControlVisitorRecord.DataSource = list;
string con ="server=127.0.0.1;database=YKTTest;uid=sa;pwd=sa";
stringcommandText = "selectPID,NAME,SEX,COMPANY,IDTYPE,IDNO,LINKWAY,REGTIME,RECEIVERNAME,RECEIVERDEPARTMENT,ACCOMPANYNUMBER,REASON,BELONGINGS,CARTAG,CARNO,PASSTYPE,CARDNO,CARDSN,VALIDITYTIME,LEAVETIME,RETURNCARDTAGFROM FK_VISITORREGISTER " + string.Format(" where REGTIME >='{0}'and REGTIME<= '{1}'", dateEditStartTime.Text, dateEditEndTime.Text);
SqlDataAdapteroda = new SqlDataAdapter(commandText, con);
dataSetVisitor.Clear();
oda.Fill(dataSetVisitor); //dataSetVisitor是界面上的一个DataSet控件
oda.Dispose();
this.gridViewVisitorRecord.BestFitColumns();
FormFactory.CloseWaitForm();
//判断是否有数据
//if (list.Count <= 0)
//{
// FormFactory.ShowMessageBoxWarningDialog(this, "无数据!");
// return;
//}
}
catch (Exception ex)
{
FormFactory.ShowMessageBoxErrorDialog(this, "加载数据失败!");
GlobalData.Instance.Log.Error(string.Format("加载数据失败!{0}", ex.Message));
}
}
存在的问题:
直接重新连接数据库进行数据的提取从而通过DataSet绑定到GridControl控件,可以大大提高绑定速度,用List需要80S的绑定用此种方法只用3S即可。但是也是存在同样的问题,不仅会出现+,而且之前通过Model中做的处理(比如离开标记的显示、卡号或者访客单号的处理)也都相当于失效了。
方案二:在绑定GridControl数据源时采用ServerMode,但是这种方式需要一个特定格式的文件,相当于有点小颠覆现在所用的三层架构,目前没有深入实验,网上说此种绑定速度比较快,但是没有切实证实。
方案三:使用DEV中提供的LinqServerModeSource和XPServerCollectionSource作为数据源,都要有相应格式文件的支持,目前没有深入测试。
- C#中优化数据查询速度
- C#使用Linq优化查询速度
- SQLite查询优化,提高获取数据速度
- 如何优化查询数据的速度
- Mysql优化查询速度进行中
- 查询速度优化
- mysql 查询速度优化
- mysql 查询速度优化
- 优化查询速度
- 优化查询速度
- 优化Hbase查询速度
- Msfconsole 优化查询速度
- 提高百万条数据的查询速度-mysql优化
- 如何优化Mysql执行查询数据的速度
- 针对MySQL提高百万条数据的查询速度优化
- msql用分表优化2亿数据查询速度
- 在SQLite中使用索引优化查询速度
- 在SQLite中使用索引优化查询速度
- OpenGL多视口变换函数:glViewport使用
- 网易云音乐的推荐音乐方式与算法漫谈
- 列出文件夹下的所有文件
- XCode7打包上传报错
- IOS UIWebView 使用 loadHTMLString 加载 特殊字符编码后的字符 方案解决
- C#中优化数据查询速度
- 解决uploadify上传丢失session问题
- 如何让Keil MDK兼容Keil C51?
- android 键盘弹出时将屏幕遮挡
- enum 学习笔记
- C#程序中获取各种目录
- 关于listview设置item高度没有效果问题
- hdu 2955 Robberies(01背包)
- android bundle存放数据详解