开发者

How to make an array flexible? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
  public List<int> GetCounts(string connectionstring)
    {
        List<int> results = new List<int>();
        string sqlStmt = "SELECT DISTINCT COUNT(Grouping) from Attendance";

        using (SqlConnection conn 开发者_Python百科= new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true"))
        using (SqlCommand cmd = new SqlCommand(conn,sqlStmt))
        {
            conn.Open();
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    int count = rdr.GetInt32(0);

                    results.Add(count);
                }
                rdr.Close();
            }
            conn.Close();
        }
        return results;
    }

Currently I have this code, I want the array to be flexible, meaning if i have more than two items in the database the array will be automatically updated. The above sql statement will retrieve the items that i want to put in the array above. If the sql statement result has more than 2 items i want the array to be updated automatically.


If you want to retrieve an arbitrary number of items from the database, I would suggest the List<T> construct over an array.

Try this:

public List<int> GetCounts(string connectionString)
{
   List<int> results = new List<int>();

   string sqlStmt = "SELECT DISTINCT COUNT(Grouping) from Attendance";

   using(SqlConnection conn = new SqlConnection(connectionString))
   using(SqlCommand cmd = new SqlCommand(sqlStmt, conn))
   {
      conn.Open();

      using(SqlDataReader rdr = cmd.ExecuteReader())
      {
          while(rdr.Read())
          {
              int count = rdr.GetInt32(0);  // read item no. 0 from the reader, as INT

              results.Add(count);
          }  

          rdr.Close();
      }

      conn.Close();
   }

   return results;
}

and then you could call this method like this:

string connStr = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;";

List<int> counts = GetCounts(connStr);

and you get back a list of all your counts - as many as there are - no dirty array hacks or anything necessary!

The List<T> construct is very flexible, too - you can have a List<string> or a list of any .NET type, really - you can build your own types (e.g. a class consisting of ten properties) and then have a list of that class type. Generics are cool! :-)


Use FieldCount property of SqlDataReader to create an array. You can also use SqlDataReader.GetValues(object[]) method to populate an array of objects with column values in current row.

Hope it helps you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜