Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all,
how to create list that store a sqlite table data and has the same columns type.
the database its not exist i create once the application is run, so the application will check if the database is exist or no then i have to store all table data in list to do some functions and equations there, i create a class with the same database table columns data type.
       // Here i want to declare a list that has same columns's type of Database table
                var Userlist = new list<user>; // users is a class i create 
                SQLiteConnection m_dbConnection;
                m_dbConnection = new SQLiteConnection("Data Source=DB.sqlite;Version=3;");
                m_dbConnection.Open();
                string sql = "select * from Users";
                SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    userlist.Add(reader);
                    // here i got error: can't convert from sqlite table to list
                    // i have (datetime) and (string) and (int) columns in the table

                }
                m_dbConnection.dispse();
// i want to be able to do the below code after database connection close
               foreach( var row in userlist)
                {
                  if(row.column's name = 1)
                    { // do something
                    }
                    
                }


What I have tried:

while (reader.Read())
{
userlist.Add(reader);

}
Posted
Updated 24-Mar-18 1:23am

1 solution

If you look at SQLiteDataReader class docs, it has methods like:
GetString(index)
GetInt32(index)

So you need to create a new user e.g.
C#
UserList.Add(new User()
{
    Name = reader.GetString(0),
    LastName = reader.GetString(1),
    // etc... (0, 1 refer to the column index)
});

If you are just starting with C#, may I recommend looking at Entity Framework Core, it can simplify database operations.
 
Share this answer
 
v2
Comments
Member 13199484 24-Mar-18 7:31am    
thanks, i will try it.
is it possible to use entity framework while i dont have existing database.
because i dont have physical database i create after the application installed on client machine
Thomas.D Williams 24-Mar-18 12:14pm    
Entity Framework has a "Code First Migrations" tool which allows you to create migrations code files from a database context (the context is just the EF class used to interact with the database).

You can use:
dbContext.Database.Migrate();
to make sure all the tables are created.

You may want to look through their overview.
https://docs.microsoft.com/en-us/ef/core/
Member 13199484 24-Mar-18 12:17pm    
grate, its really helpful, thank you.
surly i will read more about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900