OleDbDataReader 類提供了一種從數據源讀取數據記錄只進流的方法。若是想使用 SQL Server 7.0 或者更高版本,請參考文章: 使用 SQL Server 檢索數據。
OleDbDataReader myDataReader = null; OleDbConnection myConnection = new OleDbConnection("server=(local);Integrated Security=SSPI;database=northwind;provider=SQLOLEDB"); OleDbCommand myCommand = new OleDbCommand("SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees", myConnection); myConnection.Open(); myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read()) { Console.Write(myDataReader.GetInt32(0) + "\t"); Console.WriteLine(myDataReader.GetString(2) + " " + myDataReader.GetString(1) + "\t"); Console.WriteLine(myDataReader.GetString(3) + "\t"); if (myDataReader.IsDBNull(4)) Console.WriteLine("N/A\n"); else Console.WriteLine(myDataReader.GetInt32(4) + "\n"); }
// 當讀取完成時,調用 Close 方法 myDataReader.Close(); // 關閉鏈接 myConnection.Close();