buymeapc Posted January 14, 2020 Posted January 14, 2020 (edited) Hello, I was looking for a way to use the CLR functions to check a SQL 2016 database state, but I've been unable to find an example that I can get to work. Any help would be appreciated. Here's what I have, which always errors when I try to set the connection string and I'm not sure why. Thanks! #include ".\Includes\CLR.au3" #include ".\Includes\CLR Constants.au3" JustATest() Func JustATest() Local $oAssembly = _CLR_LoadLibrary("System.Data") ConsoleWrite("$oAssembly: " & IsObj($oAssembly) & @CRLF) Local $oSQLConn = _CLR_CreateObject($oAssembly, "System.Data.SqlClient.SqlConnection") ConsoleWrite("$oSQLConn: " & IsObj($oSQLConn) & @CRLF) $oSQLConn.ConnectionString = "Server=serverip;Database=dbname;UID=user;PWD=aaaa;" $oSQLConn.Open EndFunc Edit: I am using ptrex's CLR UDF from here: Edited January 14, 2020 by buymeapc Added CLR UDF
ptrex Posted January 15, 2020 Posted January 15, 2020 The challenge with .NET CLR and AutoIT is that the data type conversion between the 2 is not working out well in some cases.... Better approach is to use the PS AUTOMATION Object that works in 100% of the cases. Because the type conversion is done outside of the CLR host. Example : (Dont forget to change the database connection details) expandcollapse popup#AutoIt3Wrapper_UseX64=y #include "CLR.Au3" Local $PS_Script = "CLS" & @LF $PS_Script &= "" & @LF $PS_Script &= "$QueryStr = 'Select * From TableX'" & @LF $PS_Script &= "" & @LF $PS_Script &= "$SqlConnection = New-Object System.Data.SqlClient.SqlConnection" & @LF $PS_Script &= "" & @LF $PS_Script &= "$SqlConnection.ConnectionString = 'Server=serverip;Database=dbname;UID=user;PWD=aaaa;'" & @LF $PS_Script &= "" & @LF $PS_Script &= "$SqlCmd = New-Object System.Data.SqlClient.SqlCommand" & @LF $PS_Script &= "$SqlCmd.CommandText = $QueryStr" & @LF $PS_Script &= "" & @LF $PS_Script &= "$SqlCmd.Connection = $SqlConnection" & @LF $PS_Script &= "" & @LF $PS_Script &= "$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter" & @LF $PS_Script &= "$SqlAdapter.SelectCommand = $SqlCmd" & @LF $PS_Script &= "" & @LF $PS_Script &= "$DataSet = New-Object System.Data.DataSet" & @LF $PS_Script &= "$SqlAdapter.Fill($DataSet) " & @LF $PS_Script &= "" & @LF $PS_Script &= "'Rows # : ' + $DataSet.tables.rows.count" & @LF $PS_Script &= "$DataSet.tables.rows | Out-GridView -Title AutoIT # Export-CSV " & @LF $PS_Script &= "" & @LF $PS_Script &= "$SqlConnection.Close() " & @LF $PS_Script &= "" & @LF _Run_PSHost_Script($PS_Script) Func _Run_PSHost_Script($PSScript) Local $oAssembly = _CLR_LoadLibrary("System.Management.Automation") ConsoleWrite("!$oAssembly: " & IsObj($oAssembly) & @CRLF) ; Create Object Local $pAssemblyType = 0 $oAssembly.GetType_2("System.Management.Automation.PowerShell", $pAssemblyType) ConsoleWrite("$pAssemblyType = " & Ptr($pAssemblyType) & @CRLF) Local $oActivatorType = ObjCreateInterface($pAssemblyType, $sIID_IType, $sTag_IType) ConsoleWrite("IsObj( $oAssemblyType ) = " & IsObj($oActivatorType) & @TAB & @CRLF) ; Create Object Local $pObjectPS = 0 $oActivatorType.InvokeMember_3("Create", 0x158, 0, 0, 0, $pObjectPS) ConsoleWrite("IsObject: " & IsObj($pObjectPS) & @TAB & "$pObject: " & ObjName($pObjectPS) & @CRLF) ; <<<<<<<<<<<<<<<<<<< PS COMMAND HERE >>>>>>>>>>>>>>>>>>>> $pObjectPS.AddScript($PSScript) ; Add Script here ;~ ConsoleWrite($PSScript & @CRLF) $objAsync = $pObjectPS.BeginInvoke ; (2); ($oActivatorType,$oActivatorType) While $objAsync.IsCompleted = False ;~ ConsoleWrite($objAsync.IsCompleted & @CRLF) ContinueLoop WEnd ConsoleWrite("Completed : " & $objAsync.IsCompleted & @CRLF) $objPsCollection = $pObjectPS.EndInvoke($objAsync) ;~ ConsoleWrite("$objPsCollection: " & IsObj($objPsCollection) & @TAB & "$objPsCollection: " & ObjName($objPsCollection) & " - " & ObjName($objPsCollection,6) & " - " & ObjName($objPsCollection,3) & @CRLF) $Whnd = WinGetHandle("AutoIT") ConsoleWrite($Whnd & @CRLF) WinWaitClose($Whnd) ConsoleWrite("closed !" & @CRLF) EndFunc Enjoy ! LarsJ 1 Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New
buymeapc Posted January 21, 2020 Author Posted January 21, 2020 Thank you very much for the help with this, ptrex! This helps a lot to get me started.
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