mLipok Posted May 13 Share Posted May 13 (edited) Finally: expandcollapse popup#include <array.au3> ;~ @Humanzee question from ; https://www.autoitscript.com/forum/topic/203766-sql-ado-get-print-messages/?do=findComment&comment=1512399 ;~ https://www.autoitscript.com/forum/topic/203766-sql-ado-get-print-messages/?do=findComment&comment=1512424 ;~ https://stackoverflow.com/questions/66211062/ado-in-case-of-error-how-to-get-information-about-query-line-number Global $oADODB_Connection = ObjCreate("ADODB.Connection") _Example() Func _Example() ; Error monitoring. This will trap all COM errors while alive. ; This particular object is declared as local, meaning after the function returns it will not exist. Local $oErrorHandler = ObjEvent("AutoIt.Error", _ErrFunc) Local $_EventHandler = ObjEvent($oADODB_Connection, "__ADO_EVENT__") #forceref $oErrorHandler, $_EventHandler ; put your SQL sa password here Local $sPassword = 'AutoIt' #REMARK this script works properly only with DRIVER ;~ $oADODB_Connection.Open("DRIVER=SQL Server;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") $oADODB_Connection.Open("DRIVER=SQL Server Native Client 11.0;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") #REMARK and not wtih PROVIDER ;~ $oADODB_Connection.Open("PROVIDER=SQLOLEDB.1;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") ;~ $oADODB_Connection.Open("PROVIDER=SQL Server Native Client 11.0;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") $oADODB_Connection.Execute("PRINT 'testing message'") ; final test Local $sQuery = _ "BEGIN TRY" & @CRLF & _ " PRINT ' WATCH: BEFORE_ERROR'" & @CRLF & _ " RAISERROR ( '?????', 19, 1 )" & @CRLF & _ " PRINT ' WATCH: AFTER_ERROR'" & @CRLF & _ "END TRY" & @CRLF & _ "" & @CRLF & _ "BEGIN CATCH" & @CRLF & _ " PRINT ' WATCH: CATCH_START';" & @CRLF & _ " PRINT ' : NUMBER=' + CAST(ERROR_NUMBER() AS VARCHAR) + ' : SEVERITY=' + CAST(ERROR_SEVERITY() AS VARCHAR) + ' : STATE=' + CAST(ERROR_STATE() AS VARCHAR) + ' : LINE=' + CAST(ERROR_LINE() AS VARCHAR) + ' : MESSAGE=' + ERROR_MESSAGE();" & @CRLF & _ " PRINT ' WATCH: CATCH_END';" & @CRLF & _ "END CATCH;" & @CRLF & _ "" ConsoleWrite("- START" & @CRLF) Local $o_recordset3 = $oADODB_Connection.Execute($sQuery) ConsoleWrite("- END" & @CRLF) #forceref $o_recordset3 EndFunc ;==>_Example ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) Local $iErrorCol_Max = $oADODB_Connection.errors.Count If $iErrorCol_Max = 0 Then ConsoleWrite(@CRLF & _ "! ==> COM Error intercepted ==> NOT ADO related Error " & @CRLF & _ "-" & @TAB & "$oError.description is: " & @TAB & $oError.description & @CRLF & _ "-" & @TAB & "$oError.number is: " & @TAB & @TAB & $oError.number & " in HEX is 0x" & Hex($oError.number) & @CRLF & _ "-" & @TAB & "$oError.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ "-" & @TAB & "$oError.windescription:" & @TAB & @TAB & $oError.windescription & @CRLF & _ "-" & @TAB & "$oError.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ "-" & @TAB & "$oError.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ "-" & @TAB & "$oError.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ "-" & @TAB & "$oError.scriptline is: " & @TAB & @TAB & $oError.scriptline & @CRLF & _ "-" & @TAB & "$oError.retcode is: " & @TAB & @TAB & "0x" & Hex($oError.retcode) & @CRLF & _ "! ==> COM End of Error dump." & @CRLF & _ @CRLF) Else Local $oErr For $iErrorCol_idx = 0 To $iErrorCol_Max - 1 If $iErrorCol_idx = 0 Then ConsoleWrite(@CRLF & _ "! ==> COM Error intercepted ==> ADO Error Collection: .errors.Count = " & $iErrorCol_Max & @CRLF & _ "") $oErr = $oADODB_Connection.errors.Item($iErrorCol_idx) ConsoleWrite(@CRLF & _ ">" & @TAB & "$oErr(" & $iErrorCol_idx & ").description is: " & @TAB & $oErr.description & @CRLF & _ ">" & @TAB & "$oErr(" & $iErrorCol_idx & ").number is: " & @TAB & @TAB & $oError.number & " in HEX is 0x" & Hex($oErr.number) & @CRLF & _ ">" & @TAB & "$oErr(" & $iErrorCol_idx & ").source is: " & @TAB & @TAB & $oErr.source & @CRLF & _ ">" & @TAB & "$oErr(" & $iErrorCol_idx & ").SQLState is: " & @TAB & @TAB & $oErr.SQLState & @CRLF & _ ">" & @TAB & "$oErr(" & $iErrorCol_idx & ").NativeError is: " & @TAB & $oErr.NativeError & @CRLF & _ "-" & @TAB & "$oError.windescription:" & @TAB & @TAB & $oError.windescription & @CRLF & _ "-" & @TAB & "$oError.scriptline is: " & @TAB & @TAB & $oError.scriptline & @CRLF & _ "-" & @TAB & "$oError.helpfile is: " & @TAB & @TAB & $oError.helpfile & @CRLF & _ "-" & @TAB & "$oError.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ "-" & @TAB & "$oError.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ "-" & @TAB & "$oError.retcode is: " & @TAB & @TAB & "0x" & Hex($oError.retcode) & @CRLF & _ "-" & @TAB & "$oError.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ "") Next $oADODB_Connection.errors.clear ConsoleWrite("! ==> COM End of Error Collections dump." & @CRLF) EndIf EndFunc ;==>_ErrFunc ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name ..........: __ADO_EVENT__InfoMessage ; Description ...: ; Syntax ........: __ADO_EVENT__InfoMessage(Byref $oError, $i_adStatus, Byref $oConnection) ; Parameters ....: $oError - [in/out] an object. ; $i_adStatus - an integer value. ; $oConnection - [in/out] an object representing ADO Connection. ; Return values .: None ; Author ........: mLipok ; Modified ......: ; Remarks .......: TODO - description ; Related .......: ; Link ..........: https://msdn.microsoft.com/en-us/library/windows/desktop/ms675859(v=vs.85).aspx ; Example .......: No ; =============================================================================================================================== Func __ADO_EVENT__InfoMessage(ByRef $oError, $i_adStatus, ByRef $oConnection) #forceref $i_adStatus, $oConnection, $oError If @Compiled Then Return Local $iErrorCol_Max = $oConnection.errors.Count ConsoleWrite("! $iErrorCol_Max =" & $iErrorCol_Max & @CRLF) Local $oErr For $iErrorCol_idx = 0 To $iErrorCol_Max - 1 $oErr = $oConnection.errors.Item($iErrorCol_idx) ConsoleWrite(' [ADO InfoMessage]=' & $oErr.description & @CRLF) Next EndFunc ;==>__ADO_EVENT__InfoMessage with this SQL: BEGIN TRY PRINT ' WATCH: BEFORE_ERROR' RAISERROR ( '?????', 19, 1 ) PRINT ' WATCH: AFTER_ERROR' END TRY BEGIN CATCH PRINT ' WATCH: CATCH_START'; PRINT ' : NUMBER=' + CAST(ERROR_NUMBER() AS VARCHAR) + ' : SEVERITY=' + CAST(ERROR_SEVERITY() AS VARCHAR) + ' : STATE=' + CAST(ERROR_STATE() AS VARCHAR) + ' : LINE=' + CAST(ERROR_LINE() AS VARCHAR) + ' : MESSAGE=' + ERROR_MESSAGE(); PRINT ' WATCH: CATCH_END'; END CATCH; I get such result: >Running:(3.3.16.1):Z:\AutoItPortable\App\autoit3_x64.exe "Z:\!!!_SVN_AU3\UDF_Forum\mLipok\Examples\ADO\ADO_EXAMPLE_MS_SQL_buymeapc__Get Print Messages_v2__ADO_Error_Collections.au3" [ADO InfoMessage]=[Microsoft] Changed database context to 'master'. [ADO InfoMessage]=[Microsoft][SQL Server Native Client 11.0][SQL Server]Changed language setting to us_english. ADO EVENT fired function: __ADO_EVENT__ExecuteComplete: $iRecordsAffected=-1 $i_adStatus=1 [ADO InfoMessage]=[Microsoft][SQL Server Native Client 11.0][SQL Server]testing message - START ADO EVENT fired function: __ADO_EVENT__ExecuteComplete: $iRecordsAffected=-1 $i_adStatus=1 [ADO InfoMessage]=[Microsoft][SQL Server Native Client 11.0][SQL Server] WATCH: BEFORE_ERROR [ADO InfoMessage]=[Microsoft][SQL Server Native Client 11.0][SQL Server] WATCH: CATCH_START [ADO InfoMessage]=[Microsoft][SQL Server Native Client 11.0][SQL Server] : NUMBER=2754 : SEVERITY=16 : STATE=1 : LINE=3 : MESSAGE=Error severity levels greater than 18 can only be specified by members of the sysadmin role, using the WITH LOG option. [ADO InfoMessage]=[Microsoft][SQL Server Native Client 11.0][SQL Server] WATCH: CATCH_END - END > +>12:28:58 AutoIt3 ended. rc:0 Edited May 14 by mLipok script cleanup and comments Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24 Link to comment Share on other sites More sharing options...
mLipok Posted May 14 Share Posted May 14 WARNING: Please notice: #REMARK this script works properly only with DRIVER ;~ $oADODB_Connection.Open("DRIVER=SQL Server;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") $oADODB_Connection.Open("DRIVER=SQL Server Native Client 11.0;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") #REMARK and not wtih PROVIDER ;~ $oADODB_Connection.Open("PROVIDER=SQLOLEDB.1;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") ;~ $oADODB_Connection.Open("PROVIDER=SQL Server Native Client 11.0;SERVER=localhost\SQLExpress;uid=sa;pwd=" & $sPassword & ";") using PROVIDER you will hit the problem that only first PRINT : Local $sQuery = _ "BEGIN TRY" & @CRLF & _ " PRINT ' WATCH: BEFORE_ERROR'" & @CRLF & _ " RAISERROR ( '?????', 19, 1 )" & @CRLF & _ " PRINT ' WATCH: AFTER_ERROR'" & @CRLF & _ "END TRY" & @CRLF & _ "" & @CRLF & _ "BEGIN CATCH" & @CRLF & _ " PRINT ' WATCH: CATCH_START';" & @CRLF & _ " PRINT ' : NUMBER=' + CAST(ERROR_NUMBER() AS VARCHAR) + ' : SEVERITY=' + CAST(ERROR_SEVERITY() AS VARCHAR) + ' : STATE=' + CAST(ERROR_STATE() AS VARCHAR) + ' : LINE=' + CAST(ERROR_LINE() AS VARCHAR) + ' : MESSAGE=' + ERROR_MESSAGE();" & @CRLF & _ " PRINT ' WATCH: CATCH_END';" & @CRLF & _ "END CATCH;" & @CRLF & _ "" Will be outputed to console by __ADO_EVENT__InfoMessage() I think that this is some kind of limitation how events are captured by PROVIDER. robertocm 1 Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24 Link to comment Share on other sites More sharing options...
mLipok Posted May 23 Share Posted May 23 (edited) Please anybody test it on other DB as I test it only with MS SQL. Edited May 23 by mLipok Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now