3

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;
        }
5
  • You might want to consider using parameterised SQL instead of what you have, to avoid SQL injection attacks. Commented Dec 9, 2010 at 13:56
  • Just a thought - To simplify this you could return say a List<DailyAvgRating> or ArrayList where DailyAvgRating is a class/struct/model that contains CompanyId, and your SQL columns (e.g. Num_Rating_Day, Rating_Gem, DayMonthYear) result. Commented Dec 9, 2010 at 13:58
  • 1
    @Graham Clark: I get what you're saying and on the whole parametrised queries are safer, but since Boyd is only inputting numbers, there is no SQL injection vector here. Commented Dec 9, 2010 at 14:13
  • what do the indices of your array represent, boyd? is it arr[row, col]? Commented Dec 9, 2010 at 14:15
  • @Matt it must be filled with data to be handled by an graph class. So it must contain values for the x and y. Like { {x1, x2, x3}, {y1, y2, y3} } Commented Dec 9, 2010 at 14:18

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 });
    }
}

2 Comments

Problem is that I don't have a choice.. I need to fill the array excactly this way because that's how the data is handled by another part of the system.
This works, except mines are string instead of int. How can I sort the list by string2 (int2) in your case?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.