![]() |
VOOZH | about |
The CData Cmdlets for Couchbase 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 Couchbase.
The Cmdlets are not only a PowerShell interface to Couchbase, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Couchbase data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Couchbase. To access Couchbase data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Couchbase.
Once you have acquired the necessary connection properties, accessing Couchbase data in PowerShell can be enabled in three steps.
To connect using the Login method, set User, Password, and Server to the credentials for the account and the address of the server you want to connect to.
Install the module:
Install-Module CouchbaseCmdlets
Connect:
$couchbase = Connect-Couchbase -User "$User" -Password "$Password" -Server "$Server"
Search for and retrieve data:
$firstname = "Bob" $customer = Select-Couchbase -Connection $couchbase -Table "Customer" -Where "FirstName = `'$FirstName`'" $customer
You can also use the Invoke-Couchbase cmdlet to execute SQL commands:
$customer = Invoke-Couchbase -Connection $couchbase -Query 'SELECT * FROM Customer WHERE FirstName = @FirstName' -Params @{'@FirstName'='Bob'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Couchbase\lib\System.Data.CData.Couchbase.dll")
Connect to Couchbase:
$conn= New-Object System.Data.CData.Couchbase.CouchbaseConnection("User=myuseraccount;Password=mypassword;Server=http://mycouchbaseserver;")
$conn.Open()
Instantiate the CouchbaseDataAdapter, execute an SQL query, and output the results:
$sql="SELECT FirstName, TotalDue from Customer"
$da= New-Object System.Data.CData.Couchbase.CouchbaseDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.firstname $_.totaldue
}
Update-Couchbase -Connection $Couchbase -Columns @('FirstName','TotalDue') -Values @('MyFirstName', 'MyTotalDue') -Table Customer -Id "MyId"
$cmd = New-Object System.Data.CData.Couchbase.CouchbaseCommand("UPDATE Customer SET FirstName='Bob' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Couchbase.CouchbaseParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-Couchbase -Connection $Couchbase -Table Customer -Columns @("FirstName", "TotalDue") -Values @("MyFirstName", "MyTotalDue")
$cmd = New-Object System.Data.CData.Couchbase.CouchbaseCommand("INSERT INTO Customer (FirstName) VALUES (@myFirstName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Couchbase.CouchbaseParameter("@myFirstName","Bob")))
$cmd.ExecuteNonQuery()
Remove-Couchbase -Connection $Couchbase -Table "Customer" -Id "MyId"
$cmd = New-Object System.Data.CData.Couchbase.CouchbaseCommand("DELETE FROM Customer WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Couchbase.CouchbaseParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Couchbase Data Provider to get started:
Download NowLearn more:
👁 Couchbase IconRapidly create and deploy powerful .NET applications that integrate with Couchbase NoSQL databases.