DataTable.Rows.Find()使用心得 (转贴)

11 篇文章 0 订阅

 

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;
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值