将DataRow作为参数传递给函数 [英] Passing DataRow as an argument to a function

查看:323
本文介绍了将DataRow作为参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


请考虑以下代码片段:

Hi,
Please consider the following code snippet:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace ConsoleApplication4
{
    class Program
    {
        public DataTable DT { get; private set; }
        public const string PrimaeyKeyColumnName = @"PK";
        public const string ValueColumnName = @"Value";

        static void InitDT()
        {
            DT.Columns.Add(PrimaeyKeyColumnName, typeof(string));
            DT.Columns.Add(ValueColumnName, typeof(string));
            DT.PrimaryKey = new DataColumn[] { DT.Columns[PrimaeyKeyColumnName] };
        }

        void UpdateDT(string primaryKey, string value)
        {
            DataRow dr = null;
            lock (DT.Rows.SyncRoot)
            {
                if (DT.Rows.Contains(primaryKey))
                {
                    dr = DT.Rows.Find(primaryKey);
                    dr[ValueColumnName] = value;
                }
                else
                {
                    DT.Rows.Add(primaryKey, value);
                }
            }
            if (dr == null)
            {
                dr = DT.Rows.Find(primaryKey);
            }
            ProcessDR(dr);
        }

        void ProcessDR(DataRow dataRow)
        {
            // Do some processing based on some external conditions being true such as read dataRow[ValueColumnName] into some variable; that means within this method the read operation on the dataRow column cell will not always happen

            // Hitting this error SOMETIMES:

           //System.IndexOutOfRangeException: Index was outside the bounds of the array.
           //at System.Data.Common.StringStorage.Get(Int32 recordNo)
           //at System.Data.DataRow.get_Item(String columnName)
        }

        static void Main(string[] args)
        {
            InitDT();
            // Call UpdateDT() very frequently based on some trigger from the environment
        }
    }
}




查看ProcessDR方法.我很少/以随机方式遇到该错误(我知道这不是随机"一词的最科学用法).

我认为,将对DataRow的引用的副本传递给ProcessDR方法,并且ProcessDR访问其列单元时,有时CLR会抛出该错误,因为DataTable DT处于某种间歇"状态(请记住DataTable是经常从单独的上下文进行更新).

现在有2个问题:
1.我对错误原因的理解正确吗?
2.我应该在将DataRow传递给ProcessDR之前克隆它吗?如果可以,最有效的方法是什么?

关于




Look at the ProcessDR method. I''m hitting that error infrequently / in a random manner (I know that''s not the most scientific use of the word ''random'').

I think, as a copy of the reference to the DataRow is passed to the ProcessDR method, and while ProcessDR accesses its column cells, sometimes the CLR throws that error because of the DataTable DT being in some "intermittent" state (remember the DataTable is being updated very frequently from a separate context).

Now 2 questions:
1. Is my understanding of the cause of the error correct?
2. Should I clone the DataRow before passing it to ProcessDR? If yes, what''s the most performance-effective way to do that?

Regards

推荐答案

您极有可能试图让数据行中的某些成员为null或为空.
因此,您会收到此错误.

如果您通过逐步执行代码行来调试方法,我相信您将能够找出问题并解决问题.
You are most probably trying toa ccess some members within the datarow which are possible null or empty.
Thus you get this error.

If you debug the method by stepping through the lines of code, I''m sure you will be able to figure out the problem and solve the issue.


这篇关于将DataRow作为参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆