Reads a forward-only stream of rows from SQLite.
The following example creates a
SQLiteConnection, a
SQLiteCommand, and a
SQLiteDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the
SQLiteDataReader, then the
SQLiteConnection.
|
|---|
public void ReadMyData(string myConnString) {
SQLiteConnection sqLiteConnection = new SQLiteConnection(myConnString);
SQLiteCommand sqLiteCommand = (SQLiteCommand)sqLiteConnection.CreateCommand();
sqLiteCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Dept";
sqLiteConnection.Open();
SQLiteDataReader sqLiteReader = sqLiteCommand.ExecuteReader();
try {
// Always call Read before accessing data.
while (sqLiteReader.Read()) {
Console.WriteLine(sqLiteReader.GetInt32(0).ToString() + " " +
sqLiteReader.GetString(1) + " " + sqLiteReader.GetString(2));
}
}
finally {
// always call Close when done reading.
sqLiteReader.Close();
// Close the connection when done with it.
sqLiteConnection.Close();
}
} |
|
|---|
Public Sub ReadMyData(ByVal myConnString As String)
Dim sqLiteConnection As New SQLiteConnection(myConnString)
Dim sqLiteCommand As SQLiteCommand = sqLiteConnection.CreateCommand()
sqLiteCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Dept"
sqLiteConnection.Open()
Dim sqLiteReader As SQLiteDataReader = sqLiteCommand.ExecuteReader()
Try
' Always call Read before accessing data.
While sqLiteReader.Read()
Console.WriteLine(String.Concat(sqLiteReader.GetInt32(0).ToString(), " ", _
sqLiteReader.GetString(1), " ", sqLiteReader.GetString(2)))
End While
Finally
' always call Close when done reading.
sqLiteReader.Close()
' Close the connection when done with it.
sqLiteConnection.Close()
End Try
End Sub |