Automate MariaDB Integration Tasks from PowerShell



Are you in search of a quick and easy way to access MariaDB data from PowerShell? This article demonstrates how to utilize the MariaDB Cmdlets for tasks like connecting to MariaDB data, automating operations, downloading data, and more.

The CData Cmdlets for MariaDB 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 MariaDB.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to MariaDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MariaDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MariaDB. To access MariaDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MariaDB.

Once you have acquired the necessary connection properties, accessing MariaDB data in PowerShell can be enabled in three steps.

The Server and Port properties must be set to a MariaDB 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, the tables from all databases are reported.

PowerShell

  1. Install the module:

    Install-Module MariaDBCmdlets
  2. Connect:

    $mariadb = Connect-MariaDB -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
  3. Search for and retrieve data:

    $shipcountry = "USA" $orders = Select-MariaDB -Connection $mariadb -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders

    You can also use the Invoke-MariaDB cmdlet to execute SQL commands:

    $orders = Invoke-MariaDB -Connection $mariadb -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 MariaDB\lib\System.Data.CData.MariaDB.dll")
  2. Connect to MariaDB:

    $conn= New-Object System.Data.CData.MariaDB.MariaDBConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;") $conn.Open()
  3. Instantiate the MariaDBDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT ShipName, ShipCity from Orders" $da= New-Object System.Data.CData.MariaDB.MariaDBDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.shipname $_.shipcity }

Update MariaDB Data

PowerShell

Update-MariaDB -Connection $MariaDB -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"

ADO.NET

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

Insert MariaDB Data

PowerShell

Add-MariaDB -Connection $MariaDB -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")

ADO.NET

$cmd = New-Object System.Data.CData.MariaDB.MariaDBCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.MariaDB.MariaDBParameter("@myShipCountry","USA"))) $cmd.ExecuteNonQuery()

Delete MariaDB Data

PowerShell

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

ADO.NET

$cmd = New-Object System.Data.CData.MariaDB.MariaDBCommand("DELETE FROM Orders WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.MariaDB.MariaDBParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the MariaDB Data Provider to get started:

 Download Now

Learn more:

MariaDB Icon MariaDB ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with MariaDB databases.