faustf Posted March 16, 2016 Share Posted March 16, 2016 (edited) hi guy , and hi @mLipok i try to use the ADO udf but i have some problem my script have some personal udf , in one i us ADO expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <Array.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> #include <WinAPIFiles.au3> Global $StatusBar1, $oRecordset, $oConnection, $sConnectionString Local Const $sFilePath = @ScriptDir & "\CONFIGURE\company\DB_Account.ini" Global $sDriver = IniRead($sFilePath, "Azienda", "Key1", "Default Value") Global $sDatabase = IniRead($sFilePath, "Azienda", "Key2", "Default Value") Global $sServer = IniRead($sFilePath, "Azienda", "Key3", "Default Value") Global $sPort = IniRead($sFilePath, "Azienda", "Key4", "Default Value") Global $sUser = IniRead($sFilePath, "Azienda", "Key5", "Default Value") Global $sPassword = IniRead($sFilePath, "Azienda", "Key6", "Default Value") ; Internal ADO.au3 UDF COMError Handler _ADO_ComErrorHandler_UserFunction(_ADO_COMErrorHandler) Func _ConnectDB_gest() Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';PORT=' & $sPort & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';' _GUICtrlStatusBar_SetText($StatusBar1, "Connecting DB") _GUICtrlStatusBar_SetIcon($StatusBar1, 0, _WinAPI_ExtractIcon($DLL_LOCATE, 82)) Local $oConnection = _ADO_Connection_Create() _ADO_Connection_OpenConString($oConnection, $sConnectionString) If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE) _GUICtrlStatusBar_SetText($StatusBar1, "Offline DB") _GUICtrlStatusBar_SetIcon($StatusBar1, 0, _WinAPI_ExtractIcon($DLL_LOCATE, 80)) Else _GUICtrlStatusBar_SetText($StatusBar1, "Online DB") _GUICtrlStatusBar_SetIcon($StatusBar1, 0, _WinAPI_ExtractIcon($DLL_LOCATE, 81)) EndIf EndFunc ;==>_ConnectDB_gest Func _CloseDB_gest() ; CleanUp $oRecordset = Null _ADO_Connection_Close($oConnection) $oConnection = Null EndFunc ;==>_CloseDB_gest Func _Example_1_RecordsetToConsole($sQUERY) ; Executing some query Local $oRecordset = _ADO_Execute($oConnection, $sQUERY) ; Get recordset to array of arrays (Conent and ColumnNames) Local $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False) ; Get inner array - only conent of Recordset Local $aRecordsetContent = _ADO_RecordsetArray_GetContent($aRecordsetArray) ; Go through the array variable (Recorset Conent) Local $iColumn_count = UBound($aRecordsetContent, $UBOUND_COLUMNS) For $iRecord_idx = 0 To UBound($aRecordsetContent) - 1 ConsoleWrite('==================================================================' & @CRLF) For $iColumn_idx = 0 To $iColumn_count - 1 ConsoleWrite($aRecordsetContent[$iRecord_idx][$iColumn_idx] & @CRLF) Next Next ; Clean Up $oRecordset = Null _ADO_Connection_Close($oConnection) $oConnection = Null EndFunc ;==>_Example_1_RecordsetToConsole and in this have a script expandcollapse popup; Script Start - Add your code below here #include <AutoItConstants.au3> #include <GUIConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GuiStatusBar.au3> #include <WindowsConstants.au3> #include <WinAPIRes.au3> #include <GuiListView.au3> #include <File.au3> #include "include\ModernMenuRaw.au3" ; Only unknown constants are declared here ;#include "E:\_GESTIONALE_NEW\include\menu.au3" ; include i menu #include "include\menu.au3" ; include i menu #include "include\ADO.au3" ; include function for adodb #include "include\DB.au3" ; include function di aggancio del db + futuribile new sql language #include "include\Language.au3" ; include language #include "include\Variabili.au3" ; include variabili globali #include "include\distructor_gui.au3" ; include distruttore degli elementi sopra la gui #include "include\GUI_Configure.au3" ; include logica della gui configure #NoTrayIcon Opt("GuiOnEventMode", 1) Opt("GUICloseOnESC", 0) Opt("GUIResizeMode", 128) ;Global $Lang[10] Global $ListBox2,$CONF_Input37 _gui() Func _gui() $Form1 = GUICreate("Choices Dialog", 353, 263, 302, 218) $ListBox1 = GUICtrlCreateList("", 8, 8, 137, 201) GUICtrlSetData(-1, "Item1|Item2|Item3|Item4|Item5") $Button1 = GUICtrlCreateButton(">", 156, 15, 30, 25) $Button2 = GUICtrlCreateButton(">>", 156, 48, 31, 25) $Button3 = GUICtrlCreateButton("<", 157, 81, 31, 25) GUICtrlSetState(-1, $GUI_DISABLE) $Button4 = GUICtrlCreateButton("<<", 157, 114, 32, 25) $ListBox2 = GUICtrlCreateListView("", 200, 8, 137, 201) _GUICtrlListView_SetExtendedListViewStyle($ListBox2, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES)) ; questa stringa fa la griglia stile excel GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 220) _GUICtrlListView_InsertColumn($ListBox2, 0, "Tipologia di Documento", 150) $Button5 = GUICtrlCreateButton("&OK", 104, 225, 75, 25) $Button6 = GUICtrlCreateButton("&Cancel", 184, 225, 75, 25) ;$Button7 = GUICtrlCreateButton("&Help", 264, 225, 75, 25) $CONF_Input37 = GUICtrlCreateInput("", 264, 225, 75, 25) GUISetState(@SW_SHOW) GUISetOnEvent($GUI_EVENT_CLOSE, "_close") _Read_ini_Multi_Output(0,$ListBox2 ,'motifCONTI.ini', 'Pagamento_Conti', 'key') GUICtrlSetOnEvent($Button5, "_Cerca") ; tasto cerca per fattura tipo documento _ConnectDB_gest() _Example_1_RecordsetToConsole("SELECT * FROM ge_configurazione ") EndFunc Func _cerca() _Cerca_listview($CONF_Input37, $ListBox2) EndFunc #cs Func _Cerca_listview($input_box, $listCL) $testodacercare = GUICtrlRead($input_box) $iI = _GUICtrlListView_FindText($listCL, $testodacercare) MsgBox(4160, "Info", "Target: " & $iI&" "&$listCL&" "&$testodacercare) _GUICtrlListView_EnsureVisible($listCL, $iI) _GUICtrlListView_SetItemDropHilited($listCL, $iI) EndFunc ;==>_Cerca_listview #ce Func _close() Exit EndFunc ;==>_close ;=============================================================== ;Keep the GUI alive ;=============================================================== While 1 Sleep(1000) WEnd but not write nothing in console , like if ADO not connect you have some suggest? thankz Edited March 16, 2016 by faustf Link to comment Share on other sites More sharing options...
jchd Posted March 16, 2016 Share Posted March 16, 2016 Just browsing thru code. The variable $oConnection is local to the function _ConnectDB_gest so it's going to be destroyed when exiting from the function. This doesn't affect the global variable, which never gets assigned anything IMHO. The help clearly says: "Global can be used to assign to global variables inside a function, but if a local variable (or parameter) has the same name as a global variable, the local variable will be the only one used. It is recommended that local and global variables have distinct names." This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
faustf Posted March 16, 2016 Author Share Posted March 16, 2016 (edited) o bad but is not possible pass at second function a value?? Edited March 16, 2016 by faustf Link to comment Share on other sites More sharing options...
faustf Posted March 16, 2016 Author Share Posted March 16, 2016 i dont understund correctly , i suppose , what is IMHO ? but if i assign like global , not die after exit , just right???? Link to comment Share on other sites More sharing options...
jchd Posted March 16, 2016 Share Posted March 16, 2016 Simply remove the Local declaration in the function. To understand why, read again slowly the quoted exerpt from the help I pasted. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
faustf Posted March 16, 2016 Author Share Posted March 16, 2016 o thankz yea i not saw local decalerd for $oConnection thankz so muxh some time i sleep so mcuh Link to comment Share on other sites More sharing options...
mLipok Posted March 16, 2016 Share Posted March 16, 2016 (edited) Hi @faustf @jchd Thanks for claryfication for @faustf. I want also add my 0,02$ Func _CloseDB_gest() ; CleanUp $oRecordset = Null _ADO_Connection_Close($oConnection) $oConnection = Null EndFunc ;==>_CloseDB_gest $oRecordset - should be always local If you want to clean Up this object you should do it internally in each function. Here are my other remarks and some script refactoring: expandcollapse popup; REMARK ; Try to always use: this two line #AutoIt3Wrapper_Au3Check_Parameters and #Tidy_Parameters ; This really help in keeping script clean. #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 #Tidy_Parameters=/sort_funcs /reel #Region - MyApp - Include ; REMARK ; Learn to use #Region <> #EndRegion ; Script Start - Add your code below here #include <AutoItConstants.au3> #include <GUIConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GuiStatusBar.au3> #include <WindowsConstants.au3> #include <WinAPIRes.au3> #include <GuiListView.au3> #include <File.au3> #include "include\ModernMenuRaw.au3" ; Only unknown constants are declared here ;#include "E:\_GESTIONALE_NEW\include\menu.au3" ; include i menu #include "include\menu.au3" ; include i menu #include "include\ADO.au3" ; include function for adodb #include "include\DB.au3" ; include function di aggancio del db + futuribile new sql language #include "include\Language.au3" ; include language #include "include\Variabili.au3" ; include variabili globali #include "include\distructor_gui.au3" ; include distruttore degli elementi sopra la gui #include "include\GUI_Configure.au3" ; include logica della gui configure #EndRegion - MyApp - Include #Region - MyApp - Options #NoTrayIcon Opt("GuiOnEventMode", 1) Opt("GUICloseOnESC", 0) Opt("GUIResizeMode", 128) ;Global $Lang[10] #EndRegion - MyApp - Options Global $ListBox2, $CONF_Input37 _Main() Func _Main() ; REMARK ; Try to keep the core of script in one Main function ; This is very handy in future when you want to diagnose initailization / processing / ending section of your script ; This is really very handy to debug scripts, especialy when you have 25 projects with tens of thousands of lines of code in each projects _gui() ; REMARK ; Create connection onces at beginning _ConnectDB_gest() _Example_1_RecordsetToConsole("SELECT * FROM ge_configurazione ") ;=============================================================== ;Keep the GUI alive ;=============================================================== While 1 Sleep(1000) WEnd ; REMARK ; Close connection onces at end _closeDB_gest() _close() EndFunc Func _gui() ; REMARK: ; From my own experience: ; This is very good approach that you create GUI in separate Funcions and not in Global Scope $Form1 = GUICreate("Choices Dialog", 353, 263, 302, 218) $ListBox1 = GUICtrlCreateList("", 8, 8, 137, 201) GUICtrlSetData(-1, "Item1|Item2|Item3|Item4|Item5") $Button1 = GUICtrlCreateButton(">", 156, 15, 30, 25) $Button2 = GUICtrlCreateButton(">>", 156, 48, 31, 25) $Button3 = GUICtrlCreateButton("<", 157, 81, 31, 25) GUICtrlSetState(-1, $GUI_DISABLE) $Button4 = GUICtrlCreateButton("<<", 157, 114, 32, 25) $ListBox2 = GUICtrlCreateListView("", 200, 8, 137, 201) _GUICtrlListView_SetExtendedListViewStyle($ListBox2, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES)) ; questa stringa fa la griglia stile excel GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 220) _GUICtrlListView_InsertColumn($ListBox2, 0, "Tipologia di Documento", 150) $Button5 = GUICtrlCreateButton("&OK", 104, 225, 75, 25) $Button6 = GUICtrlCreateButton("&Cancel", 184, 225, 75, 25) ;$Button7 = GUICtrlCreateButton("&Help", 264, 225, 75, 25) $CONF_Input37 = GUICtrlCreateInput("", 264, 225, 75, 25) GUISetState(@SW_SHOW) GUISetOnEvent($GUI_EVENT_CLOSE, "_close") _Read_ini_Multi_Output(0, $ListBox2, 'motifCONTI.ini', 'Pagamento_Conti', 'key') GUICtrlSetOnEvent($Button5, "_Cerca") ; tasto cerca per fattura tipo documento EndFunc ;==>_gui Func _cerca() _Cerca_listview($CONF_Input37, $ListBox2) EndFunc ;==>_cerca #cs Func _Cerca_listview($input_box, $listCL) $testodacercare = GUICtrlRead($input_box) $iI = _GUICtrlListView_FindText($listCL, $testodacercare) MsgBox(4160, "Info", "Target: " & $iI&" "&$listCL&" "&$testodacercare) _GUICtrlListView_EnsureVisible($listCL, $iI) _GUICtrlListView_SetItemDropHilited($listCL, $iI) EndFunc ;==>_Cerca_listview #ce Func _close() Exit EndFunc ;==>_close expandcollapse popup#include-once ; REMARK ; Try to always use: this two line #AutoIt3Wrapper_Au3Check_Parameters and #Tidy_Parameters ; This really help in keeping script clean. #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 #Tidy_Parameters=/sort_funcs /reel #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <Array.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> #include <WinAPIFiles.au3> Global Const $sFilePath = @ScriptDir & "\CONFIGURE\company\DB_Account.ini" Global $oMyConnection_Gest = Null ; connection string and recordset should be in local scope Global $StatusBar1 = 0 Global $sDriver = IniRead($sFilePath, "Azienda", "Key1", "Default Value") Global $sDatabase = IniRead($sFilePath, "Azienda", "Key2", "Default Value") Global $sServer = IniRead($sFilePath, "Azienda", "Key3", "Default Value") Global $sPort = IniRead($sFilePath, "Azienda", "Key4", "Default Value") Global $sUser = IniRead($sFilePath, "Azienda", "Key5", "Default Value") Global $sPassword = IniRead($sFilePath, "Azienda", "Key6", "Default Value") ; Internal ADO.au3 UDF COMError Handler _ADO_ComErrorHandler_UserFunction(_ADO_COMErrorHandler) Func _ConnectDB_gest() Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';PORT=' & $sPort & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';' _GUICtrlStatusBar_SetText($StatusBar1, "Connecting DB") _GUICtrlStatusBar_SetIcon($StatusBar1, 0, _WinAPI_ExtractIcon($DLL_LOCATE, 82)) $oMyConnection_Gest = _ADO_Connection_Create() _ADO_Connection_OpenConString($oMyConnection_Gest, $sConnectionString) If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE) _GUICtrlStatusBar_SetText($StatusBar1, "Offline DB") _GUICtrlStatusBar_SetIcon($StatusBar1, 0, _WinAPI_ExtractIcon($DLL_LOCATE, 80)) Else _GUICtrlStatusBar_SetText($StatusBar1, "Online DB") _GUICtrlStatusBar_SetIcon($StatusBar1, 0, _WinAPI_ExtractIcon($DLL_LOCATE, 81)) EndIf EndFunc ;==>_ConnectDB_gest Func _CloseDB_gest() ; CleanUp _ADO_Connection_Close($oMyConnection_Gest) $oMyConnection_Gest = Null ; REMARK: ; Recordset is always in local scope ; only connection needs to be available globally ; $oRecordset = Null ; REMARK: ; in your application: Do not use exactly the same name for objects like in UDF ; for example $oRecordset_My or $oRecordset_Test1 or $oRecordset_CMD1 EndFunc ;==>_CloseDB_gest Func _Example_1_RecordsetToConsole($sQUERY) ; Executing some query Local $oRecordset = _ADO_Execute($oMyConnection_Gest, $sQUERY) ; Get recordset to array of arrays (Conent and ColumnNames) Local $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False) ; Get inner array - only conent of Recordset Local $aRecordsetContent = _ADO_RecordsetArray_GetContent($aRecordsetArray) ; Go through the array variable (Recorset Conent) Local $iColumn_count = UBound($aRecordsetContent, $UBOUND_COLUMNS) For $iRecord_idx = 0 To UBound($aRecordsetContent) - 1 ConsoleWrite('==================================================================' & @CRLF) For $iColumn_idx = 0 To $iColumn_count - 1 ConsoleWrite($aRecordsetContent[$iRecord_idx][$iColumn_idx] & @CRLF) Next Next ; Clean Up $oRecordset = Null ; REMARK: ; if you do not want to end it at all (for example exiting program) ; you should not close Connection ; _ADO_Connection_Close($oMyConnection_Gest) ; $oMyConnection_Gest = Null EndFunc ;==>_Example_1_RecordsetToConsole After all I think I must to change my examples - just a little > I shuld use my own remarks EDIT: Try also read * Best coding practices * This is really handy Wiki article. Edited March 16, 2016 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