Aufgabenstellung
In der Programmiersprache DXL von IBM Doors soll auf Daten eines Microsoft SQL Servers zugegriffen werden.
Ansatz
Die Verwendung der COM-Schnittstelle erlaubt Zugriff auf die ADODB.Connection. Hierbei handelt es sich um eine Microsoft Bibliothek für den Zugriff auf SQL Server.
Lösung 1
Das folgende Skript erlaubt Zugriff auf den Microsoft SQL Server und führt eine SELECT-Anweisung durch:
// Connection details
string dbServer=“SERVER“;
string dbName=“DATABASE“;
string dbUsername=“USER“;
string dbPassword=“PASS“;
string connectionString = „Data Source='“ dbServer „‚;
Initial Catalog='“ dbName „‚;User Id='“ dbUsername „‚;Password='“ dbPassword „‚;“;
OleAutoArgs oleAutoArgs=create;
OleAutoObj adodbConnection, adodbRecordset, objFields, objField;
string fieldName, result, err;
int numFields, index;
// Instantiate a new ADODB Connection object
adodbConnection = oleCreateAutoObject „ADODB.Connection“;
if (null adodbConnection)
{
print „Unable to instantiate database connection\n“;
halt;
}
// Instantiate a new ADODB Recordset object
adodbRecordset = oleCreateAutoObject „ADODB.Recordset“;
if(null adodbRecordset)
{
print „Unable to create Recordset object\n“;
halt;
}
// Connection details
// Set the provider and data source of the connection
// based on information from connectionstrings.com
olePut(adodbConnection, „Provider“, „sqloledb“);
clear oleAutoArgs;
put(oleAutoArgs, connectionString );
// „Password=“ dataPass „;“)
// Open the connection to the database
err=oleMethod(adodbConnection, „Open“, oleAutoArgs);
if(!null err „“)
{
print „Error opening database: “ err „\n“;
halt;
}
// SQL Command: Open a cursor to return all columns and rows of ‚tableName‘
clear oleAutoArgs
put(oleAutoArgs, „select * from [alarms] order by 1“) // SQL Command
put(oleAutoArgs, adodbConnection) // ACTIVE CONNECTION
put(oleAutoArgs, 1) // CURSOR TYPE – ‚adOpenKeyset‘
put(oleAutoArgs, 1) // LOCK TYPE – ‚adLockReadOnly‘
put(oleAutoArgs, 1) // OPTIONS – ‚adCmdText‘
err=oleMethod(adodbRecordset, „Open“, oleAutoArgs);
if(!null err „“)
{
print „Error opening table: “ err „\n“;
halt;
}
// From the Recordset object, list each field name (defined in database)
oleGet(adodbRecordset, „Fields“, objFields);
oleGet(objFields, „Count“, numFields);
for(index=0; index
}
print „\“\n“;
// From the Recordset object cursor, loop through and print each row
while(true)
{
result=““;
clear oleAutoArgs;
put(oleAutoArgs, 2); // StringFormat – ‚adClipString‘
put(oleAutoArgs, 1); // NumRows
put(oleAutoArgs, „\“,\““); // ColumnDelimiter
put(oleAutoArgs, „\““); // RowDelimiter
put(oleAutoArgs, „“); // NullExpr
oleMethod(adodbRecordset, „GetString“, oleAutoArgs, result);
if(length(result)<=0) break else print "\"" result "\n";
}
[/javascript]
Lösung 2
Das untere Beispiel zeigt die Kapselung in eine Funktion und den Zugriff auf Einzelwerte, bzw. auf einzelne Zellen eines Recordsets. Dieses Beispiel lässt sich ausgezeichnet in andere Bibliotheken auslagern:
OleAutoArgs args = null;
void cleanup (OleAutoObj &obj)
{
if (!null obj)
{
oleCloseAutoObject obj;
obj = null;
}
}
// some syntax helpers for arguments (from DXL standard library) …
void checkNull (string s)
{
if (!null s)
{
print „Error: “ s „\n“ dxlHere();
halt;
}
}
OleAutoArgs createArgs ()
{
if (!null args) delete args;
args = create();
return args
}
OleAutoArgs ::<-(OleAutoArgs x, int a) { put(x, a); return x }
OleAutoArgs ::<-(OleAutoArgs x, string a) { put(x, a); return x }
OleAutoArgs ::<-(OleAutoArgs x, bool a) { put(x, a); return x }
// Hier bekommt man eine Spalte des aktuellen Records als String
string stringProperty (OleAutoObj obj, string s)
{
string result = null;
checkNull oleGet (obj, s, result);
return result
}
void selectStatement(string sql, string dbServer, string dbName,
string dbUsername, string dbPassword)
{
string connectionString = "Data Source='" dbServer "';Initial Catalog='"
dbName "';User Id='" dbUsername "';Password='" dbPassword "';";
OleAutoArgs oleAutoArgs=create;
OleAutoObj adodbConnection, adodbRecordset, objFields, objField;
string fieldName, result, err;
int numFields, index;
// Instantiate a new ADODB Connection object
adodbConnection = oleCreateAutoObject "ADODB.Connection";
if (null adodbConnection)
{
print "Unable to instantiate database connection\n";
halt;
}
// Instantiate a new ADODB Recordset object
adodbRecordset = oleCreateAutoObject "ADODB.Recordset";
if(null adodbRecordset)
{
print "Unable to create Recordset object\n";
halt;
}
// Connection details
// Set the provider and data source of the connection
// based on information from connectionstrings.com
olePut(adodbConnection, "Provider", "sqloledb");
clear oleAutoArgs;
put(oleAutoArgs, connectionString );
// "Password=" dataPass ";")
// Open the connection to the database
err=oleMethod(adodbConnection, "Open", oleAutoArgs);
if(!null err "")
{
print "Error opening database: " err "\n";
halt;
}
// SQL Command: Open a cursor to return all columns and rows of 'tableName'
clear oleAutoArgs
put(oleAutoArgs, sql) // SQL Command
put(oleAutoArgs, adodbConnection) // ACTIVE CONNECTION
put(oleAutoArgs, 1) // CURSOR TYPE - 'adOpenKeyset'
put(oleAutoArgs, 1) // LOCK TYPE - 'adLockReadOnly'
put(oleAutoArgs, 1) // OPTIONS - 'adCmdText'
err=oleMethod(adodbRecordset, "Open", oleAutoArgs);
if(!null err "")
{
print "Error opening table: " err "\n";
halt;
}
// From the Recordset object, list each field name (defined in database)
oleGet(adodbRecordset, "Fields", objFields);
oleGet(objFields, "Count", numFields);
for(index=0; index
}
print „\“\n“;
// From the Recordset object cursor, loop through and print each row
while(true)
{
// Break when we are still at the end
bool bEOF = false;
checkNull oleGet(adodbRecordset, „EOF“, bEOF);
if (bEOF) break;
oleGet(adodbRecordset, „Fields“, objFields);
// Hier werden die Einzelfelder ausgelesen
oleGet(objFields, „Item“, createArgs <- 0 , objField);
string feld1 = stringProperty(objField, "Value");
oleGet(objFields, "Item", createArgs <- 1 , objField);
string feld2 = stringProperty(objField, "Value");
oleGet(objFields, "Item", createArgs <- 2 , objField);
string feld3 = stringProperty(objField, "Value");
print feld3 " " feld1 " " feld2 "\n";
checkNull oleMethod (adodbRecordset, "MoveNext");
}
cleanup adodbConnection;
cleanup adodbRecordset;
cleanup objFields;
cleanup objField;
}
// Connection details
string sql="select [alarm_id], [subsystem], [bitnumber] from [alarms] order by 1";
string dbServer="SERVER";
string dbName="DATENBANK";
string dbUsername="USER";
string dbPassword="PASS";
selectStatement(sql,dbServer,dbName,dbUsername,dbPassword);
[/javascript]