2

Following is the C# code for querying the DB to get the list of users id:

int id;
con = new SqlConnection(Properties.Settings.Default.ConnectionStr);
con.Open();
id = 180;
SqlCommand command = new SqlCommand("Select userid from UserProfile where grpid=@id", con);
command.Parameters.AddWithValue("@id", id);

using (SqlDataReader reader = command.ExecuteReader())
{
    if (reader.Read())
    {
        Console.WriteLine(String.Format("{0}", reader["userid"]));
    }
}

con.Close();

Output: 5629

Actually, the list of Users having grpid = 180 are 5629, 5684, 5694.

How can I read the results in a list or an array ?

4
  • not exactly sure what you're asking, What do you mean by "read the results in list" ? Commented May 7, 2018 at 9:58
  • 1
    try to write while (reader.Read()) instead of if (reader.Read()) Commented May 7, 2018 at 9:58
  • 1
    This shows you how. Commented May 7, 2018 at 9:59
  • Possible duplicate of How to generate List<String> from SQL query? Commented May 7, 2018 at 10:01

4 Answers 4

8

simply:

List<int> results = new List<int>();
using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        results.Add((int)reader["userid"]));
    }
}
// use results

However, you might find a tool like "Dapper" a useful time saver here

var results = con.Query<int>("Select userid from UserProfile where grpid=@id",
    new { id }).AsList();
Sign up to request clarification or add additional context in comments.

6 Comments

@CodeNotFound indeed
Thanks much for the quick help.
@JG'sSpark emphasis: I strongly recommend tools like Dapper instead of raw ADO.NET; it saves a lot of time and unnecessary bugs; I'm very biased, though
Any reference link for Dapper ? would be more useful to me
|
5

Simply define a List, and use like:

List<string> Users = new List<string>();
using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        Users.Add(reader[0].ToString());
    }
}

Comments

4

Try this :

var userIds = new List<int>();
using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        userIds.Add(reader.GetInt32(0));
    }
}

Comments

4

You could use this extension method:

public static class DbExtensions
{
    public static List<T> ToList<T>(this IDataReader reader, int columnOrdinal = 0)
    {
        var list = new List<T>();
        while(reader.Read())
            list.Add((T)reader[columnOrdinal]);
        return list;
    }
}

Now you can use it in this way:

List<int> userIdList;
using (var con = new SqlConnection(Properties.Settings.Default.ConnectionStr))
{
    using(var command = new SqlCommand("Select userid from UserProfile where grpid=@id", con))
    {
       command.Parameters.AddWithValue("@id", id);
       con.Open();
       using (SqlDataReader rd = command.ExecuteReader())
          userIdList = rd.ToList<int>();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.