![]() |
VOOZH | about |
The CData Cmdlets for CouchDB 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 CouchDB.
The Cmdlets are not only a PowerShell interface to CouchDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete CouchDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for CouchDB. To access CouchDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for CouchDB.
Once you have acquired the necessary connection properties, accessing CouchDB data in PowerShell can be enabled in three steps.
Set the following to connect:
Install the module:
Install-Module ApacheCouchDBCmdlets
Connect:
$apachecouchdb = Connect-ApacheCouchDB -Url "$Url" -User "$User" -Password "$Password"
Search for and retrieve data:
$movierating = "R" $movies = Select-ApacheCouchDB -Connection $apachecouchdb -Table "Movies" -Where "MovieRating = `'$MovieRating`'" $movies
You can also use the Invoke-ApacheCouchDB cmdlet to execute SQL commands:
$movies = Invoke-ApacheCouchDB -Connection $apachecouchdb -Query 'SELECT * FROM Movies WHERE MovieRating = @MovieRating' -Params @{'@MovieRating'='R'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for CouchDB\lib\System.Data.CData.ApacheCouchDB.dll")
Connect to CouchDB:
$conn= New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBConnection("Url=http://localhost:5984;User=abc123;Password=abcdef;")
$conn.Open()
Instantiate the ApacheCouchDBDataAdapter, execute an SQL query, and output the results:
$sql="SELECT MovieRuntime, MovieRating from Movies"
$da= New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.movieruntime $_.movierating
}
Update-ApacheCouchDB -Connection $ApacheCouchDB -Columns @('MovieRuntime','MovieRating') -Values @('MyMovieRuntime', 'MyMovieRating') -Table Movies -Id "MyId"
$cmd = New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBCommand("UPDATE Movies SET MovieRating='R' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-ApacheCouchDB -Connection $ApacheCouchDB -Table Movies -Columns @("MovieRuntime", "MovieRating") -Values @("MyMovieRuntime", "MyMovieRating")
$cmd = New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBCommand("INSERT INTO Movies (MovieRating) VALUES (@myMovieRating)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBParameter("@myMovieRating","R")))
$cmd.ExecuteNonQuery()
Remove-ApacheCouchDB -Connection $ApacheCouchDB -Table "Movies" -Id "MyId"
$cmd = New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBCommand("DELETE FROM Movies WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ApacheCouchDB.ApacheCouchDBParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the CouchDB Data Provider to get started:
Download NowLearn more:
👁 CouchDB IconRapidly create and deploy powerful .NET applications that integrate with CouchDB.