simple to use in powershell ./MySQL.ps1 -Query "select * from table1"
to using this needs MySQL connector http://dev.mysql.com/downloads/connector/net/
simple to use in powershell ./MySQL.ps1 -Query "select * from table1"
to using this needs MySQL connector http://dev.mysql.com/downloads/connector/net/
Param( | |
[Parameter( | |
Mandatory = $true, | |
ParameterSetName = '', | |
ValueFromPipeline = $true)] | |
[string]$Query | |
) | |
$MySQLAdminUserName = 'USER' | |
$MySQLAdminPassword = 'PASSWORD' | |
$MySQLDatabase = 'MYDBNAME' | |
$MySQLHost = 'MYHOSTNAME' | |
$ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" + $MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword + ";SslMode=none;database="+$MySQLDatabase | |
Try { | |
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data") | |
$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection | |
$Connection.ConnectionString = $ConnectionString | |
$Connection.Open() | |
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection) | |
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command) | |
$DataSet = New-Object System.Data.DataSet | |
$RecordCount = $dataAdapter.Fill($dataSet, "data") | |
$DataSet.Tables[0] | |
} | |
Catch { | |
Write-Host "ERROR : Unable to run query : $query `n$Error[0]" | |
} | |
Finally { | |
$Connection.Close() | |
} |