Querying an Access database
I one had an occasion when I needed to collect quite a large amount of data from a Microsoft Access database (.mdb) and process it using MATLAB and Windows. I didn't have the database toolbox so I needed to find another method.
It is possible to query and modify an Access database using ADO. The following code snip shows you how:
% Create an ADO recordset
connA = actxserver('ADODB.RecordSet');
try
% Send a query to the database
connA.Open('QUERY_GOES_HERE', DSN);
xMin = connA.GetRows;
xMin = xMin{1};
connA.Close;
catch
% If the query is unsuccessful, for what ever reason
% we can inform the user here
end
connA = actxserver('ADODB.RecordSet');
try
% Send a query to the database
connA.Open('QUERY_GOES_HERE', DSN);
xMin = connA.GetRows;
xMin = xMin{1};
connA.Close;
catch
% If the query is unsuccessful, for what ever reason
% we can inform the user here
end
If you don't like the ADO method, it is also possible to connect to an Access database using DAO:
% Browse for an Access database file.
[filename, pathname] = uigetfile('*.mdb', 'Select a database');
database = [pathname filename];
try
% Create a connection handle
h = actxserver('DAO.DBEngine.36');
% Open a database connection
db = h.OpenDatabase(database);
% Send a query
q = db.CreateQueryDef('','SELECT * FROM [table]')
% Retrieve the result
rs = q.OpenRecordset
catch
% If the query is unsuccessful, for what ever reason
% we can inform the user here
end
[filename, pathname] = uigetfile('*.mdb', 'Select a database');
database = [pathname filename];
try
% Create a connection handle
h = actxserver('DAO.DBEngine.36');
% Open a database connection
db = h.OpenDatabase(database);
% Send a query
q = db.CreateQueryDef('','SELECT * FROM [table]')
% Retrieve the result
rs = q.OpenRecordset
catch
% If the query is unsuccessful, for what ever reason
% we can inform the user here
end
