Here is the script that establishing a connection to the Mysql database with ‘select statement’
Param(
[Parameter(
Mandatory = $true,
ParameterSetName = '',
ValueFromPipeline = $true)]
[string]$Query
)
$ConnectionString="This is your connection string"
Try {
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$Con = New-Object MySql.Data.MySqlClient.MySqlConnection
$Con.ConnectionString = $ConnectionString
$Con.Open()
$MySqlCommand= New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Con)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($MySqlCommand)
$DataSet = New-Object System.Data.DataSet
$RecordCount = $DataAdapter.Fill($DataSet,"data")
$DataSet.Tables["data"] | Format-Table
$DataSet.Tables[0]
}
Catch {
Write-Host "Exception: $Query `n$Error[0]"
}
Finally {
$Con.Close()
}
If you take an error from the dll , after the try statement you can load the another mySql dll like this.
[void][system.reflection.Assembly]::LoadFrom(“C:\Program Files (x86)\MySQL\MySQL Connector Net\MySQL.Data.dll”)
Finally you can save this script and run!
.\mySqlConnection.ps1 -Query "select * from customers where bla bla "