DataTable.Rows.Find()使用心得 (转贴)
来源:互联网 发布:下载听故事软件 编辑:程序博客网 时间:2024/06/05 10:15
1.源由:
工作上的關係。欲處理的資料量滿大的有一萬六千多筆要批次量處理。在速度上必須要特別的處理,不然程式會進入「沒有反應」的狀態。
2.目的, spec. statement.
在處理的資料中,其中一段資料搬移(新增)處理中要先檢是否已存在,不存在才可加入。這時才發現查詢速度必須加速才行。
原先是使用Linq來檢查是否已存在。因速度不夠快,改用DataTable 的Find()成員函式來處理。
3.解法, solution
使用DataTable的Find()成員函式來檢查資料是否已存在。
查看MSDN後,Find()成員函式在使用前須先設定DataTable.PrimaryKey。
參考文章:
DataRowCollection.Find 方法 (Object)
http://msdn.microsoft.com/zh-tw/library/ydd48eyk(VS.80).aspx
DataTable.PrimaryKey Property
http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey.aspx
4.原碼
下面是本人實戰原碼:
private void btnShiftAddAll_Click(object sender, EventArgs e)
{
try
{
//## UI控制
this.Cursor = Cursors.WaitCursor;
if (gvSource.Rows.Count == 0) return; // 無資料,離開。
DataTable dtSource = gvSource.DataSource as DataTable;
DataTable dtTarget = gvTarget.DataSource as DataTable;
if (dtTarget.PrimaryKey.Length == 0)
{
/// 設定PK以加速Find速度。
dtTarget.PrimaryKey = new DataColumn[] { dtTarget.Columns["ast_num"] };
}
//dtTarget.Merge(dtSource);
//## 若沒有,才加入。
foreach (DataRow shiftRow in dtSource.Rows)
{
////// 原先使用Linq Query,因速度不夠,改用別的方法。
///int isExist = dtTarget.AsEnumerable().Count(
/// c => c.Field<string>("ast_num") == shiftRow.Field<string>("ast_num"));
///if (isExist == 0)
DataRow drFind = dtTarget.Rows.Find(shiftRow.Field<string>("ast_num"));
if (drFind == null)
{
dtTarget.ImportRow(shiftRow); // 資料不存在,才加入(移動資料)。
}
}
dtSource.Clear(); //
dtSource.AcceptChanges();
dtTarget.AcceptChanges();
}
finally
{
//## UI控制
this.Cursor = Cursors.Default;
}
}
- DataTable.Rows.Find()使用心得 (转贴)
- JSF DataTable使用心得
- DataTable.Select()使用心得
- DataTable.Rows 属性
- find,grep使用心得
- find命令使用心得
- .net datatable 查找一行时的方法比较datatable.rows.find vs datable.select vs dataview vs for loop
- 如何使用DataTable.Select选出来的Rows生成新的DataTable?
- 如何使用DataTable.Select选出来的Rows生成新的DataTable?
- find 命令的使用心得
- find -mtime -newermt 使用心得
- CMS之datatable插件使用心得
- jQuery dataTable 个人使用的一点心得
- asp.net 的 Datatable 使用,asp.net c# Datatable 创建 修改 查询 更新 提交 AcceptChanges Select Rows.Add(row)
- 过滤DataTable的Rows的重复值
- 过滤DataTable的Rows的重复值
- 过滤DataTable的Rows的重复值
- datatable.Rows[i].Delete()删除行
- 使用Hibernate Annotations 维护多对多关系的心得
- HTTP POST GET 本质区别详解
- HawtDB introduction
- [转]Linux 查看硬件信息
- hdu2159 动态规划
- DataTable.Rows.Find()使用心得 (转贴)
- 游戏行业中的职位:你可以做什么
- 转一篇讲内核定时器比较好的文章 .
- 简单的ThinkPHP 二级联动
- Media enumeration constants
- Linux下进程间通信(小结)下
- Qt学习之路_10(Qt中statusBar,MessageBox和Timer的简单处理)
- Maven环境的快速搭建
- C#连接各种数据库语句(实例)