![]() |
VOOZH | about |
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.
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.
Install the module:
Install-Module MariaDBCmdlets
Connect:
$mariadb = Connect-MariaDB -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
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'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for MariaDB\lib\System.Data.CData.MariaDB.dll")
Connect to MariaDB:
$conn= New-Object System.Data.CData.MariaDB.MariaDBConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
$conn.Open()
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 -Connection $MariaDB -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"
$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()
Add-MariaDB -Connection $MariaDB -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")
$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()
Remove-MariaDB -Connection $MariaDB -Table "Orders" -Id "MyId"
$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()
CodeProject
Download a free trial of the MariaDB Data Provider to get started:
Download NowLearn more:
👁 MariaDB IconRapidly create and deploy powerful .NET applications that integrate with MariaDB databases.