Hello! |
Re: OLE ADODB.Connection help with Connection.Execute Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: OLE ADODB.Connection help with Connection.Execute Mathias Mamsch - Sat Aug 25 12:33:13 EDT 2012 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS Thank you for your reply. I want to have a working snipplet doing it, i already tried it for hours but i think there is sth. wrong (maybe in my understanding?). Can you give me a snipplet with ADODB.Connection doing a simple INSERT/UPDATE/DELETE? I think i then would see what was my problem. This throws an exception: int executeQuery(string sql, string connectionString) { OleAutoArgs oleAutoArgs=create; OleAutoObj adodbConnection; string fieldName, result, err; int numFields, index; // Instantiate a new ADODB Connection object adodbConnection = oleCreateAutoObject "ADODB.Connection"; // Instantiate a new ADODB Recordset object adodbRecordset = oleCreateAutoObject "ADODB.Recordset"; if (null adodbConnection) { print "Unable to instantiate database connection\n"; return -1; } // Connection details // Set the provider and data source of the connection // based on information from connectionstrings.com olePut(adodbConnection, "Provider", "sqloledb"); // SQL Command: Open a cursor to return all columns and rows of 'tableName' clear oleAutoArgs put(oleAutoArgs, sql) // SQL Command put(oleAutoArgs, 1) // ACTIVE CONNECTION err=oleMethod(adodbRecordset, "Execute", oleAutoArgs); if(!null err "") { print "Error opening table: " err "\n"; return -1; } cleanup adodbConnection; return 1; } |
Re: OLE ADODB.Connection help with Connection.Execute Bjoern_Karpenstein - Sun Aug 26 06:24:18 EDT 2012 err=oleMethod(adoDbConnection, "Execute", oleAutoArgs); |
Re: OLE ADODB.Connection help with Connection.Execute Bjoern_Karpenstein - Sun Aug 26 06:25:51 EDT 2012
Hmm .. the following works fine for me. Its an example for creating an access database (for test purposes) and inserting stuff into it. Hope it helps, regards, Mathias OleAutoObj objADOXCatalog = null, objADODBConnection = null, objRecordSet = null, objFields = null,objField = null OleAutoArgs args = null // cleanup stuff void cleanup (OleAutoObj &obj) { if (!null obj) oleCloseAutoObject obj; obj = null } void cleanup () { cleanup objRecordSet; cleanup objFields; cleanup objField; cleanup objADOXCatalog; cleanup objADODBConnection; } // some syntax helpers for arguments (from DXL standard library) ... void checkNull (string s) { if (!null s) { print "Error: " s "\n" dxlHere(); cleanup; 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 } string stringProperty (OleAutoObj obj, string s) { string result = null; checkNull oleGet (obj, s, result); return result } string sFile = "C:\\temp\\test2.mdb" // delete the file if it already exists deleteFile(sFile) // Create an Access Database, just for testing. You can leave this out. objADOXCatalog = oleCreateAutoObject "ADOX.Catalog"; checkNull ((null objADOXCatalog) ? "Could not create ADOX Catalog." : "") string sDatabaseString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" sFile ";Jet OLEDB:Engine Type=5" checkNull oleMethod (objADOXCatalog, "Create" , createArgs <- sDatabaseString) // Connect using ADODB ... objADODBConnection = oleCreateAutoObject "ADODB.Connection"; checkNull ((null objADODBConnection) ? "Could not create ADODB Connection." : "") string stConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" sFile ";" checkNull oleMethod (objADODBConnection, "Open" , createArgs <- stConnection) // Note the syntax here: the empty comment with following '-' must not have a space behind it! string sTableCreate = "CREATE TABLE testTable (" // - "ID COUNTER PRIMARY KEY, " // - "Test TEXT(255) " // - ")" checkNull oleMethod (objADODBConnection, "Execute" , createArgs <- sTableCreate) string sInsert = "INSERT into testTable (Test) VALUES ('Hello Database')" checkNull oleMethod (objADODBConnection, "Execute" , createArgs <- sInsert) string sItems[] = {"Hello", "World", "!"}; int i; for (i = 0; i < sizeof sItems; i++) { string sInsert = "INSERT into testTable (Test) VALUES ('" sItems[i] "')" checkNull oleMethod (objADODBConnection, "Execute" , createArgs <- sInsert) } string sSelect = "select [Test] from [testTable]" checkNull oleMethod (objADODBConnection , "Execute", createArgs <- sSelect, objRecordSet) int count = 0 while (true) { bool bEOF = false; checkNull oleGet(objRecordSet, "EOF", bEOF) if (bEOF) break print "Processing Item " (count++) ":\n" checkNull oleGet(objRecordSet, "Fields", objFields) checkNull oleGet(objFields, "Item", createArgs <- 0, objField) print " Name:" stringProperty (objField, "Value") "\n" checkNull oleMethod (objRecordSet, "MoveNext") } cleanup
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: OLE ADODB.Connection help with Connection.Execute Mathias Mamsch - Mon Aug 27 04:38:10 EDT 2012
Hmm .. the following works fine for me. Its an example for creating an access database (for test purposes) and inserting stuff into it. Hope it helps, regards, Mathias OleAutoObj objADOXCatalog = null, objADODBConnection = null, objRecordSet = null, objFields = null,objField = null OleAutoArgs args = null // cleanup stuff void cleanup (OleAutoObj &obj) { if (!null obj) oleCloseAutoObject obj; obj = null } void cleanup () { cleanup objRecordSet; cleanup objFields; cleanup objField; cleanup objADOXCatalog; cleanup objADODBConnection; } // some syntax helpers for arguments (from DXL standard library) ... void checkNull (string s) { if (!null s) { print "Error: " s "\n" dxlHere(); cleanup; 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 } string stringProperty (OleAutoObj obj, string s) { string result = null; checkNull oleGet (obj, s, result); return result } string sFile = "C:\\temp\\test2.mdb" // delete the file if it already exists deleteFile(sFile) // Create an Access Database, just for testing. You can leave this out. objADOXCatalog = oleCreateAutoObject "ADOX.Catalog"; checkNull ((null objADOXCatalog) ? "Could not create ADOX Catalog." : "") string sDatabaseString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" sFile ";Jet OLEDB:Engine Type=5" checkNull oleMethod (objADOXCatalog, "Create" , createArgs <- sDatabaseString) // Connect using ADODB ... objADODBConnection = oleCreateAutoObject "ADODB.Connection"; checkNull ((null objADODBConnection) ? "Could not create ADODB Connection." : "") string stConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" sFile ";" checkNull oleMethod (objADODBConnection, "Open" , createArgs <- stConnection) // Note the syntax here: the empty comment with following '-' must not have a space behind it! string sTableCreate = "CREATE TABLE testTable (" // - "ID COUNTER PRIMARY KEY, " // - "Test TEXT(255) " // - ")" checkNull oleMethod (objADODBConnection, "Execute" , createArgs <- sTableCreate) string sInsert = "INSERT into testTable (Test) VALUES ('Hello Database')" checkNull oleMethod (objADODBConnection, "Execute" , createArgs <- sInsert) string sItems[] = {"Hello", "World", "!"}; int i; for (i = 0; i < sizeof sItems; i++) { string sInsert = "INSERT into testTable (Test) VALUES ('" sItems[i] "')" checkNull oleMethod (objADODBConnection, "Execute" , createArgs <- sInsert) } string sSelect = "select [Test] from [testTable]" checkNull oleMethod (objADODBConnection , "Execute", createArgs <- sSelect, objRecordSet) int count = 0 while (true) { bool bEOF = false; checkNull oleGet(objRecordSet, "EOF", bEOF) if (bEOF) break print "Processing Item " (count++) ":\n" checkNull oleGet(objRecordSet, "Fields", objFields) checkNull oleGet(objFields, "Item", createArgs <- 0, objField) print " Name:" stringProperty (objField, "Value") "\n" checkNull oleMethod (objRecordSet, "MoveNext") } cleanup
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Thank you very much! That works for me. oleGet(adodbRecordset, "Fields", objFields); oleGet(objFields, "Count", numFields); for(index=0; index<numFields; index++) { clear oleAutoArgs; string theType = null; put(oleAutoArgs, index); oleGet(objFields, "Item", oleAutoArgs, objField); oleGet(objField, "Name", fieldName); oleGet(objField, "Type", theType); print (index>0?"\",":"") "\"" fieldName "( " theType " )"; // <--- theType is empty??? } |
Re: OLE ADODB.Connection help with Connection.Execute Bjoern_Karpenstein - Fri Aug 31 04:53:07 EDT 2012
Thank you very much! That works for me. oleGet(adodbRecordset, "Fields", objFields); oleGet(objFields, "Count", numFields); for(index=0; index<numFields; index++) { clear oleAutoArgs; string theType = null; put(oleAutoArgs, index); oleGet(objFields, "Item", oleAutoArgs, objField); oleGet(objField, "Name", fieldName); oleGet(objField, "Type", theType); print (index>0?"\",":"") "\"" fieldName "( " theType " )"; // <--- theType is empty??? } http://msdn.microsoft.com/en-us/library/windows/desktop/ms675318%28v=vs.85%29.aspx Regards, Mathias Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: OLE ADODB.Connection help with Connection.Execute Mathias Mamsch - Fri Aug 31 06:04:15 EDT 2012 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: OLE ADODB.Connection help with Connection.Execute Mathias Mamsch - Fri Aug 31 06:18:00 EDT 2012 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS I casted it as Integer and now it works. Thank you very much |