开发者

Using DataTables and storing values

I am new to c#.net. i have a method where i pass parameters in a loop and for each parameter there are different rows returned . rows (which have data of different data types) from database. I want to store that data rows somewhere like arraylist. and use for furthue peocessing.

Plz tell me how to do this. enter code here

/*ideally Get_ChildAndParentInfo(int pointid) function returns array list but how to deal with array list containing datarows with different data types */

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;

    }


It looks you're trying to return an ArrayList of ArrayLists. It would be better to return a Generic List instead of an ArrayList.

ArrayList Collect = new ArrayList();

Should be

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

Custom Class:

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

(Please forgive my bad C# syntax. I'm a VB.NET guy.)


ArrayList list = new ArrayList();

//inside your loop add:

list.Add(myparameter);

You can create different lists based on what type of data you have.

EDIT:

Create a class which contains properties for each datatype a row contains and then make a List<MyObject> list and you can then add a new object for each item, assigning each column for each row to the right property.

Like such:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜