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 sqConnection = new SQLiteConnection(myConnString);
SQLiteCommand sqCommand = (SQLiteCommand)sqConnection.CreateCommand();
sqCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Dept";
sqConnection.Open();
SQLiteDataReader sqReader = sqCommand.ExecuteReader();
try {
// Always call Read before accessing data.
while (sqReader.Read()) {
Console.WriteLine(sqReader.GetInt32(0).ToString() + " " +
sqReader.GetString(1) + " " + sqReader.GetString(2));
}
}
finally {
// always call Close when done reading.
sqReader.Close();
// Close the connection when done with it.
sqConnection.Close();
}
}
Public Sub ReadMyData(ByVal myConnString As String)
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As SQLiteCommand = sqConnection.CreateCommand()
sqCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Dept"
sqConnection.Open()
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
Try
' Always call Read before accessing data.
While sqReader.Read()
Console.WriteLine(String.Concat(sqReader.GetInt32(0).ToString(), " ", _
sqReader.GetString(1), " ", sqReader.GetString(2)))
End While
Finally
' always call Close when done reading.
sqReader.Close()
' Close the connection when done with it.
sqConnection.Close()
End Try
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2