开发者

Adding an value to multidimensional array in c# asp.net

I need to add values in an multidimensional array whitin an while loop. But I don't know how.

public Array getDailyAvgRatingByCompanyId(int companyId, int periodStart = 0, int periodEnd = 0)
        {
            int[,] arr = { { }, { } };

            string queryString = "SELECT num_ratings_day, rating_gem, daymonthyear FROM company_rating_daily_avg WHERE company_id开发者_运维技巧 = " + companyId + " ORDER BY daymonthyear ASC";
            SqlDataReader myDataReader = Database.sqlDataReader(queryString);

            if (myDataReader.HasRows)
            {
                while (myDataReader.Read())
                {
                    //Something like arr[0].Push(myDataReader['num_ratings_day']
                }
            }

            return arr;
        }


This assumes that you want all the columns in the query put into the array, and that all the columns return ints.

public Array getDailyAvgRatingByCompanyId(int companyId, int periodStart = 0, int periodEnd = 0)
{
    List<int[]> rowList = new List<int[]>();

    string queryString = "SELECT num_ratings_day, rating_gem, daymonthyear FROM company_rating_daily_avg WHERE company_id = " + companyId + " ORDER BY daymonthyear ASC";
    SqlDataReader myDataReader = Database.sqlDataReader(queryString);

    if (myDataReader.HasRows)
    {
        while (myDataReader.Read())
        {
            int[] values = new int[3];
            values[0] = myDataReader['num_ratings_day'];
            values[1] = myDataReader['rating_gem'];
            values[2] = myDataReader['daymonthyear'];
            rowList.add(values);
        }
    }

    int[,] arr = new int[rowList.Count, 3];

    for(int i = 0; i < rowList.Count; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            arr[i,j] = rowList[i][j];
        }
    }

    return arr;
}


You should use a data type other than Array for this.

Try using a List of arrays:

var list = new List<int[]>();

if (myDataReader.HasRows)
{
    while (myDataReader.Read())
    {
        var int1 = ??; // Get data 1
        var int2 = ??; // Get data 2

        list.Add(new[] { int1, int2 });
    }
}


You could use a DataTable by creating a SqlDataAdapter (with SELECT Statement), and than DataAdapter.Fill(DataTable). So you retrieve a DataTable with all values returned by sql statement

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜