Automate CockroachDB Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

CockroachDB ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with CockroachDB.



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

Cmdlets or ADO.NET?

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

After obtaining the needed connection properties, accessing CockroachDB data in PowerShell consists of three basic 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

  1. Install the module:

    Install-Module CockroachDBCmdlets
  2. Connect:

    $cockroachdb = Connect-CockroachDB -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
  3. 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

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for CockroachDB\lib\System.Data.CData.CockroachDB.dll")
  2. Connect to CockroachDB:

    $conn= New-Object System.Data.CData.CockroachDB.CockroachDBConnection("User=root;Password=root;Database=system;Server=localhost;Port=26257") $conn.Open()
  3. 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()