Reading all records from sqlite local file using c#

145 Views Asked by At

I am trying to read all the records from a local sqlite file. But for some reason, the code that I am using isn't reading all records. It is reading only the second row, that too only few columns, not all columns as well from that row.

Below are the records that my sample sqlite file has.

1|ABC - Student1|1E|TX|01122017|BLK|120|9210001010|20121|sample1
1|DEF - Student2|2E|AZ|01112017|BLK|110|9210002020|20122|sample2
1|GHI - Student3|3E|TX|01102017|BLK|130|9210003030|20123|sample3
1|JKL - Student4|4E|AZ|01092017|BLK|100|9210004040|20124|sample4
1|MNO - Student5|5E|TX|01082017|BLK|140|9210005050|20125|sample5

When I open my sample file in notepad, below is how I see the data:

1ABC - Student11ETX01122017BLK120921000101020121sample1K
1DEF - Student22EAZ01112017BLK110921000202020122sample2Q
1GHI - Student33ETX01102017BLK130921000303020123sample3P
1JKL - Student44EAZ01092017BLK100921000404020124sample4Y
1MNO - Student55ETX01082017BLK140921000505020125sample5

When I try to read all the records from code, it is fetching only the 2nd record, that too not all columns. It is fetching only 5 columns. Below is the code that I am using to read the file:

public List<string> ReadStuent()
        {
            var conn = new SQLiteConnection(@"Data Source=C:\Sqlite\Students.sqlite");
            SQLiteDataReader datareader;
            
            int count = 0;
            var StudentData= new List<string>();
            conn.Open();
            
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = "SELECT * FROM Students";
            

            datareader = cmd.ExecuteReader();
            while (datareader.Read())
            {
                StudentData.Add(datareader.GetString(count));
                count++;
            }
            conn.Close();
            return StudentData;
        }

Can someone help me what wrong I am doing here?

2

There are 2 best solutions below

1
Justin Kirk On
public List<string> ReadStuent()
        {
            var conn = new SQLiteConnection(@"Data Source=C:\Sqlite\Students.sqlite");
            SQLiteDataReader datareader;
            
          
            var StudentData= new List<string>();
            conn.Open();
            
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = "SELECT * FROM Students";
            

            datareader = cmd.ExecuteReader();
            while (datareader.Read())
            {
                StudentData.Add(datareader["nameoffieldhere"].ToString());
             
            }
            conn.Close();
            return StudentData;
        }
0
quaabaam On

Below is a basic example to to get you going forward. There are other problems with your approach but I will leave those details out and focus on your code example.

// The datareader.Read() below sets the current row and will increment
// the current row with each loop iteration
while (datareader.Read())
{
    // iterate all columns on the datareader to get values
    for(var i = 0;i < datareader.FieldCount; i++)
    {
        // get current column from current row
        studentData.Add(datareader.GetString(i));
    }
}