Ready to get started?

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

Download Now

Automate MariaDB Integration Tasks from PowerShell

Are you looking for a quick and easy way to access MariaDB data from PowerShell? We show how to use the Cmdlets for MariaDB and the CData ADO.NET Provider for MariaDB to connect to MariaDB data and synchronize, automate, download, 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.

Cmdlets or ADO.NET?

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

After obtaining the needed connection properties, accessing MariaDB data in PowerShell consists of three basic 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 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 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 System.Data.CData.MariaDB.MariaDBParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()