From: Syed Md. Galib on
Hello,
I am stuck with this problem for last few weeks. I have the codes to connect to the database and then import data from there to MATLab workspace. then I performed some calculations and now I need to save this data to another table in the same database. again, i am using same connection (as i have the connection which worked for importing). But now it's not working. it's "access" database. i am giving the codes which i found. please, help me about this. i need this very urgent.
thank you in advance.
regards,
Galib
////////////////////////////////////////////////////////
code: oledbcn
/////////////////////
function cn=oledbcn(cnstr,cto,rst)
% cn = oledb(cnstr,[cto],[rst])
%
% Connects to OLEDB using the Microsoft ActiveX Data Source Control
%
% Inputs:
% cnstr, str containing information for connecting to data source
% cto, CommandTimeout in seconds (default=60 seconds if unspecified)
% rst, RecordsetType: 1=dscSnapshot (default), 2=dscUpdatableSnapshot
%
% Output:
% cn, connection
%
% Notes: Refer to demo_oledb.m and oledbcnstr.m for example connection
% strings for Oracle, SQL, and MS Access databases
%
% Tim Myers
% myers(a)metronaviation.com
% March 2005
% Updated January 2006
% Updated August 2006

try
%create activeX control
try
cn=COM.OWC11_DataSourceControl_11;
catch
cn=COM.OWC10_DataSourceControl_10;
end

%Open connection
cn.ConnectionString=cnstr;

%Specify connection timeout if provided
if nargin>1
cn.Connection.CommandTimeout=cto;
else
cn.Connection.CommandTimeout=60; %default
end

%Specify connection RecordsetType
if nargin>2
cn.RecordsetType=rst;
else
cn.RecordsetType=1; %dscSnapshot default
end
catch
disp('***** OLEDBCN TROUBLESHOOTING *****')
disp('Could not create connection.')
disp('Verify your system has access to one of the ')
disp('following ActiveX controls: ');
disp(' COM.OWC10_DataSourceControl_10');
disp(' COM.OWC11_DataSourceControl_11');
disp('To view your available ActiveX controls enter: ');
disp(' [h, info] = actxcontrolselect');
disp('Look for the current version of "Microsoft Office Data Source Control" ');
disp('and verify the "Program ID" matches the one used on line 26 of oledbcn.m');
disp('If that does not solve the problem, verify the connection string is valid ');
disp('by searching "universal data link" or "DSN-less connection strings" in Google');
error('Could not create connection. See troubleshooting above.')
end

//////////////////////////////////////////////////////////
code: oledbcnstr
///////////////////
function s=oledbcnstr(type,sv,db,uid,pwd)
% s=oledbcnstr(type,[sv],[db],[uid],[pwd])
%
% Returns connection string for OLEDB connection
%
% Inputs:
% type, connection type - currently supported: 'Access','SQL','Oracle'
% sv, name of server - not required for Access connection
% db, name of database - not required for Oracle connection
% uid, user id - added to connection string if provided
% pwd, password - added to connection string if provided
%
% Output:
% s, connection string
%
% Usage:
% s=oledbcnstr('access',[],'test.mdb')
% s=oledbcnstr('sql','servername','databasename','userid','password')
%
% Tim Myers
% myers(a)metronaviation.com
% March 2005


if strcmpi('ACCESS',type)
%Connect to Access database given by input db
s=['PROVIDER=MSDASQL;'];
s=[s 'DRIVER={Microsoft Access Driver (*.mdb)};'];
s=[s 'DBQ=' db ';'];

elseif strcmpi('SQL',type)
%Connect to SQL Database
s=['PROVIDER=MSDASQL;DRIVER={SQL Server};'];
s=[s 'SERVER=' sv ';DATABASE=' db ';'];

elseif strcmpi('ORACLE',type)
%Connect to Oracle Database
s=['PROVIDER=MSDASQL;'];
s=[s 'DRIVER={Microsoft ODBC for Oracle};'];
s=[s 'SERVER=' sv ';'];
else
s='Unknown type';
end

%add uid and pwd if provided
if nargin>3 s=[s 'UID=' uid ';']; end
if nargin>4 s=[s 'PWD=' pwd ';']; end

/////////////////////////////////////////////////////////
code: oledbquery
///////////////////////
function x=oledbquery(cn,sql,flag)
% [x]=oledbquery(cn,sql)
%
% oledbquery Executes the sql statement against the connection cn
%
% Inputs:
% cn, open connection to OLEDB ActiveX Data Source Control
% sql, SQL statement to be executed
%
% Output
% x, cell array of query results
%
% Notes: Convert cells to strings using char. Convert cells to numeric
% data using cell2mat() for ints or double(cell2mat()) for floats
%
% Tim Myers
% myers(a)metronaviation.com
% March 2005

%open recordset and run query
r = cn.connection.invoke('execute', sql);

%if for retrieving or inserting data
if flag == 1 % i tried here not to get data again. %
%retrieve data from recordset
if r.state && r.recordcount>0
x=invoke(r,'getrows');
x=x';
else
x=[];
end
end
%release recordset
invoke(r,'release');

//////////////////////////////////////////////////////////
code: oledb_demo
/////////////////////
%demo_oledb
% Demonstrates the OLEDB connection functions
%
% Requires these files:
% oledbcnstr.m
% oledbcn.m
% oledbquery.m
% test.mdb
%
% Usage:
% Only 4 lines to connect, query, and disconnect:
%
% s=oledbcnstr(type,[sv],[db],[uid],[pwd]);
% cn = oledb(cnstr,[cto],[rst]);
% [x]=oledbquery(cn,sql);
% invoke(cn,'release')
%
% Tim Myers
% myers(a)metronaviation.com
% March 2005
clc
disp('Make sure test.mdb is in the current directory')
%Build connection string
%Google "dsn-less connection strings" for help constructing
%your own connection strings
s=oledbcnstr('Access',[],[cd '\test.mdb']);
%Open connection
cn=oledbcn(s);
%Sample query to execute
% for i=1:20
% for j=13:15
sql='select distinct spiraldataid,X,Y,time,radius,theta,DR,DT from spiraldata where questionid = 13 and testdataid = 1' ;
flag = 1
%Run query and return results
x=oledbquery(cn,sql,flag)
X = double(cell2mat(x(:,2)))
Y = double(cell2mat(x(:,3)))
T = cell2mat(x(:,4))
R = double(cell2mat(x(:,5)))
theta = double(cell2mat(x(:,6)))
DR = double(cell2mat(x(:,7)))
DT = double(cell2mat(x(:,8)))
dMean = mean(T)
dStdDev = std(T)
flag = 0
count = oledbquery(cn,sql,flag)
icount = cell2mat(count)
icount = icount + 1
% insert(cn, 'Features', 'ID', icount)
% insert(cn, 'Features', 'TestDataID', i)
% insert(cn, 'Features', 'QuestionID', j)
% insert(cn, 'Features', 'Mean', dMean)
%*****************************
% when i tried with this insert function it showed error % with connection
%***********************
sql = 'insert into Features values (icount, 1, 13, dMean)'
x = oledbquery(cn,sql,flag)
%**********************************
%in this case also error was shown
%*********************************
% end
% end
%Close connection
invoke(cn,'release')