How to get SQL query result in a list
public class users
{
public string user_id { get; set; }
public string user_name { get; set; }
}
________________________________________
public List<users> gettest()
{
List<users> columnData = new List<users>();
using (SqlConnection connection = new SqlConnection(GetConnectionString()))
{
connection.Open();
string query = "select user_id,user_name from employee_table";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var person = new users();
person.user_id = reader["user_id"].ToString();
person.user_name = reader["user_name"].ToString();
columnData.Add(person);
}
}
}
connection.Close();
}
return columnData;
}
{
List<users> columnData = new List<users>();
using (SqlConnection connection = new SqlConnection(GetConnectionString()))
{
connection.Open();
string query = "select user_id,user_name from employee_table";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var person = new users();
person.user_id = reader["user_id"].ToString();
person.user_name = reader["user_name"].ToString();
columnData.Add(person);
}
}
}
connection.Close();
}
return columnData;
}