Store the results in memory (List) using c#

I want to store many record that I query from database in list , one record has 10 fields, in order to loop it later. But I have no idea to do that. Anyone can answer me please.


Below is a good practice to store data and loop through among them.

Create Model/POCO class as:

public class DataClassName
    public int Id { get; set; }
    public string Name { get; set; }
    //Create properties according to your fields

Fill and get data list:

public List<DataClassName> GetDataList()
    List<DataClassName> dataList = new List<DataClassName>();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from TargetTableName";
    cmd.CommandType = CommandType.Text;
        using (SqlConnection connection = 
                   new SqlConnection("YourConnectionString"))
            cmd.Connection = connection;
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
                while (reader.Read())
                        new DataClassName()
                                Id = Convert.ToInt32(reader["ID"]),
                                Name = Convert.ToString(reader["Name"])
                                //Set all property according to your fields
    catch(Exception ex_)
        //Handle exception
    return dataList;

Save data that is returned from GetDataList() into your datalist and loop through among the data as required.

Here's how you'd go about storing it in a DataTable:

SqlConnection conn = new SqlConnection("yourConnectionStringHere");
SqlCommand GetData = new SqlCommand();
GetData.Connection = conn;
GetData.CommandText = "select * from yourTable"; // or whatever your query is, whether ad hoc or stored proc
// add parameters here if your query needs it

SqlDataAdapter sda = new SqlDataAdapter(GetData);
DataTable YourData = new DataTable();


If you have 10 fields, you'd be hard-pressed to store your data in a List<T> object. Your best bet would be to create a class tailored to the data you are looking to retrieve (if you want to take it a step further than the DataTable implementation above) with corresponding properties/fields.

Perhaps you could give a bit more information...

If you use the entity framework or similar to query the database, it will probably return an enumerable object.. you just need to call .ToList() on this to save it as a list.

Do you mean that you want to store this across web requests? Then you could store it in the HttpRuntime.Cache collection, allowing for it to expire after some time.

Alternatively store it in a static property. Session is also an option but it doesn't sound like the best option for this

Need Your Help

Trying to get textView to stick to gridView scroll

android android-layout android-gridview android-scroll

I have a fragment with a textView giving instructions, and below a gridView. I wanted for the textView to scroll upwards with the gridView. In other words: for textView to disappear off screen when

Howto determine file owner on windows using python without pywin32

python windows

I am writing a script which needs to determine the username of the owner of files on windows.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.