使用DataTables并存储值

时间:2010-01-15 12:36:04

标签: asp.net c#-3.0

我是c#.net的新手。 我有一个方法,我在循环中传递参数,并为每个参数返回不同的行。 数据库中的行(具有不同数据类型的数据)。 我想将数据行存储在像arraylist这样的地方。并用于进行加工。

Plz告诉我该怎么做。 enter code here

/ *理想情况下Get_ChildAndParentInfo(int pointid)函数返回数组列表但是如何处理包含不同数据类型的数据行的数组列表* /

public static ArrayList Get_ChildAndParentInfo(int PointID) 
    {
        string Sp_name = "USP_Get_Parents";
        SqlParameter[] Parameters = new SqlParameter[1];
        Parameters[0] = new SqlParameter("@IntPointId", DbType.Int32);
        DataTable dtChildInfo = new DataTable();
        ArrayList ChildNParents = new ArrayList();
        ArrayList Collect = new ArrayList();

        int i = 0;


        Parameters[0].Value = PointID;
              dtChildInfo = DataLayer.getdata1(Sp_name, Parameters);

            //  for (i = 0; i < dtChildInfo.Rows.Count; i++)
            //  {
            //      ArrayList temp = new ArrayList();
            //      for (int j = 0; j < dtChildInfo.Columns.Count; j++)
            //      {
            //          temp.Add(dtChildInfo.Rows[0][j]);
            //      }
            //      //Collect[i] = temp;
            //      Collect.Insert(i, temp);
            //      temp.Clear();
            //}


              //PrintValues(Collect);
        return (Collect);
    }
    public static ArrayList send_SMS() **///ENTRY POINT FUNCTION**
    {

        ArrayList Points = new ArrayList();
        DataTable PathInfo = new DataTable();
        ArrayList ParentInfo = new ArrayList();
        PathInfo = Get_ActivePath(); 
        Points = GetPoints(PathInfo);**//returns 6,3**
        for (int i = 0; i < Points.Count; i++)
        {
            //ParentInfo = Get_ChildAndParentInfo();

            ParentInfo = Get_ChildAndParentInfo(Convert.ToInt32(Points[i]));

           PrintValues(ParentInfo);
        }


        return ParentInfo;

    }

2 个答案:

答案 0 :(得分:2)

看起来你正在尝试返回ArrayLists的ArrayList。最好返回Generic List而不是ArrayList。

ArrayList Collect = new ArrayList();

应该是

List<CustomClass> Collect = new List<CustomClass>();

自定义类:

class CustomClass
{
    private ArrayList _ChildData;
    public void Insert(ArrayList Data)
    {
        _ChildData.Add(Data);
    }
    public ArrayList ChildData {
        get { return _ChildData; }
    }
}

(请原谅我糟糕的C#语法。我是VB.NET的人。)

答案 1 :(得分:0)

ArrayList list = new ArrayList();

//inside your loop add:

list.Add(myparameter);

您可以根据自己的数据类型创建不同的列表。

编辑:

创建一个包含行所包含的每个数据类型的属性的类,然后创建一个List<MyObject>列表,然后为每个项添加一个新对象,将每一行的每一列分配给右侧属性。

像这样:

mylist.Add(new MyObject() { MyInt = datatable.rows[i][j].Value; //etc })