Reputation: 360
I'm trying to query data from an access file, but it always returns nothing. I've already copy the query string and query directly in Microsoft Access, it returns value just fine. Here are my source code, please take a look.
public DataTable queryDB(string querystr) {
DataTable res;
string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.mdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(conStr);
con.Open();
OleDbCommand cmd = new OleDbCommand(querystr, con);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read()) {
res = new DataTable();
res.Load(reader);
}
else
res = null;
reader.Close();
con.Close();
return res;
}
The query is just "SELECT * FROM USERINFO;". I've already spent 2 hours at this, please anyone give me some clues.
Upvotes: 0
Views: 59
Reputation: 378
Another approach to get the DataTable:
public DataTable queryDB(string querystr) {
DataTable res = new DataTable();
string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.mdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(conStr);
con.Open();
OleDbDataAdapter= new OleDbDataAdapter(querystr, con);
oDataAdapter.Fill(res);
oDataAdapter.Dispose();
con.Close();
return res;
}
Upvotes: 2
Reputation: 95
Is this on Windows 8? I've seen strange behavior where a query to an Access database only returns some of the records that it should. For us it was only returning 34 or so records out of thousands.
It has no other errors?
If it has no other errors and it's not on Windows 8 I would say the answer lies in the Connection String.
Upvotes: 0