-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConnectingToExternalDatabase.xpp
79 lines (60 loc) · 2.24 KB
/
ConnectingToExternalDatabase.xpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Author:Mafigu Huggins
//Tel: +263 782 326 160
//Email: [email protected]
//If we need to interface any external database with Dynamics AX, we can achieve this task by using ADO and its AX available classes:
//CCADOConnection, CCADORecordSet, CCADOFields, CCADOField and CCADOCommand. Here an example:
class ConnectToExternalDatabase
{
static void ADOTestJob(Args _args)
{
CCADOConnection ccConnection;
CCADOCommand ccCommand;
CCADORecordSet ccRecordset;
CCADOFields ccFields;
str st;
str data1;
int data2;
;
ccConnection = new CCADOConnection();
// Setting the connection string
ccConnection.connectionString(StrFmt('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=%3;Password=%4;Initial Catalog=%2;Data Source=%1'
, 'servername' // Server's IP or name
, 'database' // Database or catalog
, 'user' // Username
, 'pwd' // Password
));
// Open the connection
ccConnection.open();
// Preparing the query
st = "SELECT * FROM mytable";
// Recordset object creation
ccRecordset = new CCADORecordSet();
// Executing the query
ccRecordset.open( st, ccConnection );
// Reading data
while (!ccRecordset.EOF())
{
ccFields = ccRecordset.fields();
// We can access fields either by name or by Index
data1 = ccFields.itemName("FIELD1").value();
data2 = ccFields.itemIdx(1).value();
info(strfmt("Data %1, %2", data1, data2));
// Read next record
ccRecordset.moveNext();
}
// Closing the connection
ccRecordset.close();
ccConnection.close();
}
//If we need to execute something (an UPDATE, DELETE, etc.), we can use the CCADOCommand:
void ExecuteSQLExt(str sql)
{
// Creating the ADO Command object
ccCommand = new CCADOCommand();
// Associate it with an existing opened connection
ccCommand.activeConnection(ccConnection);
// Executing the command
ccCommand.commandText(SQL);
ccCommand.execute();
}
}