Ready to get started?

Learn more about the CData ADO.NET Provider for MySQL or download a free trial:

Download Now

Automate MySQL Integration Tasks from PowerShell

Are you looking for a quick and easy way to access MySQL data from PowerShell? We show how to use the Cmdlets for MySQL and the CData ADO.NET Provider for MySQL to connect to MySQL data and synchronize, automate, download, and more.

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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the MySQL API, 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.

After obtaining the needed connection properties, accessing MySQL data in PowerShell consists of three basic steps.

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.

PowerShell

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect:

    $mysql = Connect-MySQL -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
  3. 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'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for MySQL\lib\System.Data.CData.MySQL.dll")
  2. Connect to MySQL:

    $conn= New-Object System.Data.CData.MySQL.MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;") $conn.Open()
  3. 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 Data

PowerShell

Update-MySQL -Connection $MySQL -Columns @('ShipName','Freight') -Values @('MyShipName', 'MyFreight') -Table Orders -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.MySQL.MySQLParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert MySQL Data

PowerShell

Add-MySQL -Connection $MySQL -Table Orders -Columns @("ShipName", "Freight") -Values @("MyShipName", "MyFreight")

ADO.NET

$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn) $cmd.Parameters.Add(new System.Data.CData.MySQL.MySQLParameter("@myShipCountry","USA")) $cmd.ExecuteNonQuery()

Delete MySQL Data

PowerShell

Remove-MySQL -Connection $MySQL -Table "Orders" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.MySQL.MySQLCommand("DELETE FROM Orders WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.MySQL.MySQLParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()