access数据库取不到数据
Cant get data from access data base
我是 c# 新手,当我尝试连接访问数据库时,我得到一个空数据库和空表,尽管它们有数据。
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());
来自documentation for the OleDbDataReader.Read()
method:
The default position of the OleDbDataReader is before the first
record. Therefore, you must call Read to start accessing any data.
因此,您需要像这样访问结果行的属性:
while (reader.Read())
Console.Out.WriteLine(reader["Id_fk"].ToString());
我是 c# 新手,当我尝试连接访问数据库时,我得到一个空数据库和空表,尽管它们有数据。
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());
来自documentation for the OleDbDataReader.Read()
method:
The default position of the OleDbDataReader is before the first record. Therefore, you must call Read to start accessing any data.
因此,您需要像这样访问结果行的属性:
while (reader.Read())
Console.Out.WriteLine(reader["Id_fk"].ToString());