![]() |
VOOZH | about |
The CData Cmdlets for MySQL are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MySQL.
The Cmdlets are not only a PowerShell interface to MySQL, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MySQL data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MySQL. To access MySQL data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MySQL.
Once you have acquired the necessary connection properties, accessing MySQL data in PowerShell can be enabled in three steps.
The CData Provider supports connecting to on-premises and cloud-hosted versions of MySQL such as Amazon RDS for MySQL, Google Cloud SQL for MySQL, Azure Database for MySQL, or Oracle MySQL HeatWave. The Server and Port properties must be set to a MySQL server. If IntegratedSecurity is set to false, then User and Password must be set to valid user credentials. Optionally, Database can be set to connect to a specific database. If not set, tables from all databases will be returned.
You can use SSH (Secure Shell) to authenticate with MySQL, whether the instance is hosted on-premises or in supported cloud environments. SSH authentication ensures that access is encrypted (as compared to direct network connections).
To connect to MySQL via SSH in Password Auth mode, set the following connection properties:
To connect to MySQL via SSH in Password Auth mode, set the following connection properties:
Install the module:
Install-Module MySQLCmdlets
Connect:
$mysql = Connect-MySQL -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
Search for and retrieve data:
$shipcountry = "USA" $orders = Select-MySQL -Connection $mysql -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders
You can also use the Invoke-MySQL cmdlet to execute SQL commands:
$orders = Invoke-MySQL -Connection $mysql -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for MySQL\lib\System.Data.CData.MySQL.dll")
Connect to MySQL:
$conn= New-Object System.Data.CData.MySQL.MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
$conn.Open()
Instantiate the MySQLDataAdapter, execute an SQL query, and output the results:
$sql="SELECT ShipName, Freight from Orders"
$da= New-Object System.Data.CData.MySQL.MySQLDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.shipname $_.freight
}
Update-MySQL -Connection $MySQL -Columns @('ShipName','Freight') -Values @('MyShipName', 'MyFreight') -Table Orders -Id "MyId"
$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MySQL.MySQLParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-MySQL -Connection $MySQL -Table Orders -Columns @("ShipName", "Freight") -Values @("MyShipName", "MyFreight")
$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MySQL.MySQLParameter("@myShipCountry","USA")))
$cmd.ExecuteNonQuery()
Remove-MySQL -Connection $MySQL -Table "Orders" -Id "MyId"
$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MySQL.MySQLParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the MySQL Data Provider to get started:
Download NowLearn more:
👁 MySQL IconRapidly create and deploy powerful .NET applications that integrate with MySQL-compatible database engines.