none
LoadDataRow和Select RRS feed

  • 问题

  • 请教:

    修改某行数据,是使用DataTable select 定位后修改,还是LoatDataRow。

    LoatDataRow在使用过程中容易出现问题。批量更新表中数据的时候,是通过Select查找定位还是LoatDataRow或者其他的。

    使用上有什么区别,或各自的优缺点?获取还有别的方法可以使用?

    谢谢

    2013年10月9日 6:50

答案

  • 看源代码:

    1)DataTable's LoadDataRow:同时查询数据,并且统一修改行状态为Unchanged。

     public DataRow LoadDataRow(object[] values, bool fAcceptChanges)
            {
                DataRow item;
                DataRow dataRow;
                IntPtr intPtr;
                Bid.ScopeEnter(out intPtr, "<ds.DataTable.LoadDataRow|API> %d#, fAcceptChanges=%d{bool}\n", this.ObjectID, fAcceptChanges);
                try
                {
                    if (!this.inDataLoad)
                    {
                        item = this.UpdatingAdd(values);
                        if (fAcceptChanges)
                        {
                            item.AcceptChanges();
                        }
                        dataRow = item;
                    }
                    else
                    {
                        int num = this.NewRecordFromArray(values);
                        if (this.loadIndex != null)
                        {
                            int num1 = this.loadIndex.FindRecord(num);
                            if (num1 != -1)
                            {
                                int record = this.loadIndex.GetRecord(num1);
                                item = this.recordManager[record];
                                item.CancelEdit();
                                if (item.RowState == DataRowState.Deleted)
                                {
                                    this.SetNewRecord(item, item.oldRecord, DataRowAction.Rollback, false, true, false);
                                }
                                this.SetNewRecord(item, num, DataRowAction.Change, false, true, false);
                                if (fAcceptChanges)
                                {
                                    item.AcceptChanges();
                                }
                                dataRow = item;
                                return dataRow;
                            }
                        }
                        item = this.NewRow(num);
                        this.AddRow(item);
                        if (fAcceptChanges)
                        {
                            item.AcceptChanges();
                        }
                        dataRow = item;
                    }
                }
                finally
                {
                    Bid.ScopeLeave(ref intPtr);
                }
                return dataRow;
            }

    2) DataTable's Select:不修改行状态,仅仅为获取数据。

    public DataRow[] SelectRows() { Range range; bool flag = true; this.InitCandidateColumns(); if (!(this.expression is BinaryNode)) { this.linearExpression = this.expression; } else { this.AnalyzeExpression((BinaryNode)this.expression); if (!this.candidatesForBinarySearch) { this.linearExpression = this.expression; } if (this.linearExpression != this.expression) { flag = !this.FindClosestCandidateIndex(); } else { for (int i = 0; i < (int)this.candidateColumns.Length; i++) { if (this.candidateColumns[i] != null) { this.candidateColumns[i].equalsOperator = false; this.candidateColumns[i].expr = null; } } } } if (this.index == null && ((int)this.IndexFields.Length > 0 || this.linearExpression == this.expression)) { flag = !this.FindSortIndex(); } if (this.index == null) { this.CreateIndex(); flag = false; } if (this.index.RecordCount == 0) { return this.table.NewRowArray(0); } if (this.matchedCandidates == 0) { range = new Range(0, this.index.RecordCount - 1); this.linearExpression = this.expression; return this.GetLinearFilteredRows(range); } range = this.GetBinaryFilteredRecords(); if (range.Count == 0) { return this.table.NewRowArray(0); } if (this.matchedCandidates < this.nCandidates) { this.BuildLinearExpression(); } if (!flag) { return this.GetLinearFilteredRows(range); } this.records = this.GetLinearFilteredRecords(range); this.recordCount = (int)this.records.Length; if (this.recordCount == 0) { return this.table.NewRowArray(0); } this.Sort(0, this.recordCount - 1); return this.GetRows(); }

    要获取数据,还可以用DataView中的RowFilter属性设置条件过滤,您可以参考:

    http://msdn.microsoft.com/zh-cn/library/system.data.dataview.rowfilter.aspx


    Click For donating:Free Rice For the poor
    For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
    For more things to talk about? StackOverFlow is your choice.

    2013年10月9日 9:18
    版主