AX 2009: Error while using Connection/Userconnection for direct sql execution.

Hi,

I’ve used a Connection and Userconnection to execute statement directly, but I got the error, following is the code and error.

server static void main(Args args)
{
Userconnection connection;
Statement st;
Resultset rt;
str sql;
;

sql = " select * from CustTable";
connection = new userconnection();
st = connection.createstatement();
rt = st.executequery(sql);
while(rt.next())
{
print rt.getstring(2);
}
}

Error:
Request for the permission of type ‘SqlStatementExecutePermission’ failed.
(S)\Classes\SqlStatementExecutePermission\demand
(S)\Classes\Statement\executequery
(S)\Classes\Class1\main - line 12

Thanks,
Ambanna Yatnal

Hi,

First you have to initialize the userconnection object then you execute the select statement.

Saju.K

You missed SQL code access security.

Here is what you can try:

new SqlStatementExecutePermission(sql).assert();

rt = st.executequery(sql);

CodeAccessPermission::revertAssert();

Hope this help.