Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate CockroachDB Integration Tasks from PowerShell
Are you in search of a quick and easy way to access CockroachDB data from PowerShell? This article demonstrates how to utilize the CockroachDB Cmdlets and the CData ADO.NET Provider for CockroachDB for tasks like connecting to CockroachDB data, automating operations, downloading data, and more.
The CData Cmdlets for CockroachDB 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 CockroachDB.
PowerShell Cmdlets or ADO.NET Driver?
The Cmdlets are not only a PowerShell interface to CockroachDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete CockroachDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for CockroachDB. To access CockroachDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for CockroachDB.
Once you have acquired the necessary connection properties, accessing CockroachDB data in PowerShell can be enabled in three steps.
Set the following to connect to CockroachDB.
- Server: The host name or IP address of the server.
- Port: The port number of the CockroachDB server. If not specified, the default port is 26257.
- Database: The name of the Cockroach database. If not specified, you connect to the user's default database.
- User: The Cockroach DB user account used to authenticate.
- Password: The password used to authenticate the user.
PowerShell
-
Install the module:
Install-Module CockroachDBCmdlets
-
Connect:
$cockroachdb = Connect-CockroachDB -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
-
Search for and retrieve data:
$shipcountry = "USA" $orders = Select-CockroachDB -Connection $cockroachdb -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders
You can also use the Invoke-CockroachDB cmdlet to execute SQL commands:
$orders = Invoke-CockroachDB -Connection $cockroachdb -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for CockroachDB\lib\System.Data.CData.CockroachDB.dll")
-
Connect to CockroachDB:
$conn= New-Object System.Data.CData.CockroachDB.CockroachDBConnection("User=root;Password=root;Database=system;Server=localhost;Port=26257") $conn.Open()
-
Instantiate the CockroachDBDataAdapter, execute an SQL query, and output the results:
$sql="SELECT ShipName, ShipCity from Orders" $da= New-Object System.Data.CData.CockroachDB.CockroachDBDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.shipname $_.shipcity }
Update CockroachDB Data
PowerShell
Update-CockroachDB -Connection $CockroachDB -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.CockroachDB.CockroachDBCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add(new System.Data.CData.CockroachDB.CockroachDBParameter("@myId","10456255-0015501366"))
$cmd.ExecuteNonQuery()
Insert CockroachDB Data
PowerShell
Add-CockroachDB -Connection $CockroachDB -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")
ADO.NET
$cmd = New-Object System.Data.CData.CockroachDB.CockroachDBCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add(new System.Data.CData.CockroachDB.CockroachDBParameter("@myShipCountry","USA"))
$cmd.ExecuteNonQuery()
Delete CockroachDB Data
PowerShell
Remove-CockroachDB -Connection $CockroachDB -Table "Orders" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.CockroachDB.CockroachDBCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add(new System.Data.CData.CockroachDB.CockroachDBParameter("@myId","001d000000YBRseAAH"))
$cmd.ExecuteNonQuery()
CodeProject