I've been trying to execute an sql query and return the resultant reader but it's not behaving the way I think it should.
private SQLiteDataReader genRandom()
{
string generate = "SELECT * FROM Questions";
SQLiteConnection conn = dbConnectTest();
conn.Open();
SQLiteCommand Command = new SQLiteCommand(generate, conn);
SQLiteDataReader dreader = Command.ExecuteReader();
genMarks=0;
if (dreader.HasRows)
{ //This if-block returns 'YES' message
if (dreader.HasRows)
MessageBox.Show("YES");
else
MessageBox.Show("NO");
while (dreader.Read())
{
genMarks += dreader.GetInt32(3);
}
//But this if-block returns 'NO'
if (dreader.HasRows)
MessageBox.Show("YES");
else
MessageBox.Show("NO");
}
else
{
MessageBox.Show("No rows Found");
}
return dreader;
conn.Close();
}
The 'dreader' becomes empty, right after while-block is executed. What am I doing wrong here?
No kidding the reader is empty after you read all the rows.
The reader is doing exactly what it is supposed to do.
It is a fire hose. When the data comes out the end then it is no longer in the hose.
There is no purpose to returning that dreader as it is empty and the connection is closed.
You probably want to return a
List<int>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.