Dec 3, 2013

SQLConnection program in AX

server static void main(Args args)
{
Connection con = new Connection();
Statement stmt = con.createStatement();
ResultSet r;
str sql;
SqlStatementExecutePermission perm;
;

sql = strfmt('SELECT VALUE FROM SQLSYSTEMVARIABLES');

// Set code access permission to help protect the use of
// Statement.executeUpdate.
perm = new SqlStatementExecutePermission(sql);
perm.assert();

try
{
r = stmt.executeQuery(sql);
while (r.next())
{
info(Strfmt("%1", r.getString(1)));
}
}
catch (exception::Error)
{
Error("An error occured in the query.");
}
// Code access permission scope ends here.
CodeAccessPermission::revertAssert();
}

Writing values to CSV in AX

static void testWriteToCSV(Args _args)
{
#File
CommaTextIo commaTextIo;
FileIOPermission permission;
SalesTable sales;
str fileName = @"C:\MyFileName.csv";
;
permission = new FileIOPermission(fileName,#io_write);
permission.assert();
commaTextIo = new CommaTextIo(fileName,#io_write);
while select sales
{
commaTextIo.writeExp([Sales.SalesId,Sales.CustAccount]);
}
CodeAccessPermission::revertAssert();

}


How to Write/ Read the Comma seperated values to/from a text file ?

The following sample code gives idea that how to write or read the values to a text file

static void testWriteAndRead(Args _args)
{
CommaIo io;
container con;
FileIoPermission perm;
int i;
;
#define.MyFile(@"c:\MyFileName.txt")
#define.MyFileWrite("w")
#define.MyFileRead("r")

perm = new FileIoPermission(#MyFile, #MyFileWrite);
if (perm == null)
{
return;
}
// Grants permission to execute the CommaIo.new method.
// CommaIo.new runs under code access security.
perm.assert();

// Write
io = new CommaIo(#MyFile, #MyFileWrite);

con = [1,"MyText",1.324,"Last field"]; // Assign the entries in the container according to record layout.
io.writeExp(con); // write this record according to file format (record/field delimiters).
// End Write

//Read
io = new CommaIo(#MyFile, #MyFileRead);
if (io != null)
{
con = io.read();
for(i=1;i<=conLen(con);i++)
{
info(strFmt("%1",conpeek(con,i)));
}
}
//End Read
// Close the code access permission scope.
CodeAccessPermission::revertAssert();

}

Array in AX


Arrays hold the values of any single type

static void testArray(Args _args)
{
Array ary = new Array (Types::String);
int i;
;
ary.value(1, "abc");
ary.value(2, "xyz");
ary.value(5, "ijk");
ary.value(3, "lmn");

info(strfmt("%1", ary.toString()));
info(strfmt("%1", ary.definitionString()));
info(strfmt("%1", ary.lastIndex())); // Return the last index value of the Array

for(i=1;i<=ary.lastIndex();i++)
{
info(strfmt("%1", ary.value(i)));
}

}