Automate MariaDB Integration Tasks from PowerShell
The CData ADO.NET Provider for MariaDB is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MariaDB.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for MariaDB; this tutorial shows how to use the Provider to create, retrieve, update, and delete MariaDB data.
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.
-
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 Data
$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
$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
$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