15.12.2007, 20:35 | #1 |
Участник
|
C# and AX Development: Using the COM Business Connector in C#, the easy way (AX/Axapta)
Источник: http://olondono.blogspot.com/2007/12...in-c-easy.html
============== The easy way to perform tasks with the COM Business Connector in Axapta/AX is creating a class in the AOT and calling it from C#. Here I will show the hard way and the easy way, the axample queries the CustTable for read his name. 1. THE HARD WAY //---------------------------------- // Logon Axapta2Class ax = new Axapta2Class(); ax.Logon("username", "", "", "myConfig"); //---------------------------------- //---------------------------------- // Make the query IAxaptaObject query; IAxaptaObject queryrun; IAxaptaObject querybuilddatasource; IAxaptaObject querybuildrange; query = ax.CreateObject("Query", null, null, null, null, null, null); querybuilddatasource = (IAxaptaObject) query.Call("AddDataSource", 77, null, null, null, null, null); // CustTable Id = 77 querybuildrange = (IAxaptaObject)querybuilddatasource.Call("AddRange", 1, null, null, null, null, null); // AccountNum Id = 1 querybuildrange.Call("value", "3507", null, null, null, null, null); // 3507 is my custom query value queryrun = ax.CreateObject("QueryRun", query, null, null, null, null, null); queryrun.Call("prompt", null, null, null, null, null, null); //---------------------------------- //---------------------------------- // Process result if ((bool)queryrun.Call("next", null, null, null, null, null, null)) { IAxaptaRecord custtable = (IAxaptaRecord)queryrun.Call("getNo", 1, null, null, null, null, null); //---------------------------------- // Show customer's name MessageBox.Show((string)custtable.get_field("Name")); //---------------------------------- if (Marshal.IsComObject(custtable)) Marshal.ReleaseComObject(custtable); custtable = null; } //---------------------------------- // CleanUp if (Marshal.IsComObject(query)) Marshal.ReleaseComObject(query); query = null; if (Marshal.IsComObject(queryrun)) Marshal.ReleaseComObject(queryrun); queryrun = null; if (Marshal.IsComObject(querybuilddatasource)) Marshal.ReleaseComObject(querybuilddatasource); querybuilddatasource = null; if (Marshal.IsComObject(querybuildrange)) Marshal.ReleaseComObject(querybuildrange); querybuildrange = null; //---------------------------------- //---------------------------------- // Logoff ax.Logoff(); if (Marshal.IsComObject(ax)) Marshal.ReleaseComObject(ax); ax = null; //---------------------------------- //---------------------------------- // Release AxCOM.dll for close this session definitely ApiWin32.CoFreeUnusedLibraries(); //---------------------------------- 2. THE EASY WAY //---------------------------------- // Logon Axapta2Class ax = new Axapta2Class(); ax.Logon("olondono", "", "", "Axapta9"); //---------------------------------- //---------------------------------- // Make a call to our class and get the Customer IAxaptaObject classTest = ax.CreateObject("COMTest", null, null, null, null, null, null); IAxaptaRecord custtable = (IAxaptaRecord)classTest.Call("getCust", "3507", null, null, null, null, null); //---------------------------------- //---------------------------------- // Show customer's name MessageBox.Show((string)custtable.get_field("Name")); //---------------------------------- //---------------------------------- // CleanUp if (Marshal.IsComObject(custtable)) Marshal.ReleaseComObject(custtable); custtable = null; if (Marshal.IsComObject(classTest)) Marshal.ReleaseComObject(classTest); classTest = null; //---------------------------------- //---------------------------------- // Logoff ax.Logoff(); if (Marshal.IsComObject(ax)) Marshal.ReleaseComObject(ax); ax = null; //---------------------------------- //---------------------------------- // Release AxCOM.dll for close this session definitely ApiWin32.CoFreeUnusedLibraries(); //---------------------------------- And the Axapta class looks like: class COMTest { } public CustTable getCust(str _custAccount) { Query q; QueryRun qr; QueryBuildDataSource qbds; QueryBuildRange qrange; TableId _tableId; CustTable _custtable; ; _tableId = tablename2id("CustTable"); q = new Query(); qbds = q.addDataSource(_tableId); qrange = qbds.addRange(fieldname2id(_tableId, "AccountNum")); qrange.value(_custAccount); qr = new QueryRun(q); qr.interactive(false); qr.prompt(); if (qr.next()) _custtable = qr.getNo(1); return _custtable; } Why I prefer the easy way? because I can change the Axapta class without affect the C# application anyway. And coding AX Applications with AX busines rules is more naturally in the AOT than in C#, normally, we use the C# application as an interface, so, code must be kiss (Keep It Simple and Stupid). Источник: http://olondono.blogspot.com/2007/12...in-c-easy.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|