Ready to get started?

Learn more about the CData ADO.NET Provider for IBM Cloud SQL Query or download a free trial:

Download Now

Automate IBM Cloud SQL Query Integration Tasks from PowerShell

Are you looking for a quick and easy way to access IBM Cloud SQL Query data from PowerShell? We show how to use the Cmdlets for IBM Cloud SQL Query and the CData ADO.NET Provider for IBM Cloud SQL Query to connect to IBM Cloud SQL Query data and synchronize, automate, download, and more.

The CData Cmdlets for IBM Cloud SQL Query 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 IBM Cloud SQL Query.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the IBM Cloud SQL Query API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete IBM Cloud SQL Query data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for IBM Cloud SQL Query. To access IBM Cloud SQL Query data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for IBM Cloud SQL Query.

After obtaining the needed connection properties, accessing IBM Cloud SQL Query data in PowerShell consists of three basic steps.

IBM Cloud SQL uses the OAuth and HMAC authentication standards. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.

PowerShell

  1. Install the module:

    Install-Module IBMCloudSQLQueryCmdlets
  2. Connect:

    $ibmcloudsqlquery = Connect-IBMCloudSQLQuery -Api Key "$Api Key" -Instance CRN "$Instance CRN" -Region "$Region" -Schema "$Schema" -OAuth Client Id "$OAuth Client Id" -OAuth Client Secret "$OAuth Client Secret"
  3. Search for and retrieve data:

    $userid = "user@domain.com" $jobs = Select-IBMCloudSQLQuery -Connection $ibmcloudsqlquery -Table "Jobs" -Where "UserId = `'$UserId`'" $jobs

    You can also use the Invoke-IBMCloudSQLQuery cmdlet to execute SQL commands:

    $jobs = Invoke-IBMCloudSQLQuery -Connection $ibmcloudsqlquery -Query 'SELECT * FROM Jobs WHERE UserId = @UserId' -Params @{'@UserId'='user@domain.com'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryConnection("Api Key=MyAPIKey;Instance CRN=myInstanceCRN;Region=myRegion;Schema=mySchema;OAuth Client Id=myOAuthClientId;OAuth Client Secret=myOAuthClientSecret;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the IBMCloudSQLQueryDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Id, Status from Jobs" $da= New-Object System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.status }

Update IBM Cloud SQL Query Data

PowerShell

Update-IBMCloudSQLQuery -Connection $IBMCloudSQLQuery -Columns @('Id','Status') -Values @('MyId', 'MyStatus') -Table Jobs -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryCommand("UPDATE Jobs SET UserId='user@domain.com' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert IBM Cloud SQL Query Data

PowerShell

Add-IBMCloudSQLQuery -Connection $IBMCloudSQLQuery -Table Jobs -Columns @("Id", "Status") -Values @("MyId", "MyStatus")

ADO.NET

$cmd = New-Object System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryCommand("INSERT INTO Jobs (UserId) VALUES (@myUserId)", $conn) $cmd.Parameters.Add(new System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryParameter("@myUserId","user@domain.com")) $cmd.ExecuteNonQuery()

Delete IBM Cloud SQL Query Data

PowerShell

Remove-IBMCloudSQLQuery -Connection $IBMCloudSQLQuery -Table "Jobs" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryCommand("DELETE FROM Jobs WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.IBMCloudSQLQuery.IBMCloudSQLQueryParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()