[英]Cant get data from access data base
我是C#的新手,當我嘗試連接Access db時,我得到了一個空的db和一個空的表,盡管它們具有數據。
static void Main(string[] args)
{
String connectioString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Documents\Documents\School.accdb";
OleDbConnection MyConn = new OleDbConnection(connectioString);
MyConn.Open();
OleDbDataReader reader = null;
OleDbCommand command = new OleDbCommand("SELECT * FROM Students",MyConn);
reader = command.ExecuteReader();
Console.Out.WriteLine(reader["Id_fk"].ToString());
從OleDbDataReader.Read()
方法的文檔中 :
OleDbDataReader的默認位置在第一條記錄之前。 因此,您必須調用Read才能開始訪問任何數據。
因此,您需要像這樣訪問結果行的屬性:
while (reader.Read())
Console.Out.WriteLine(reader["Id_fk"].ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.