![]() |
VOOZH | about |
The CData Cmdlets for Sybase 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 Sybase.
The Cmdlets are not only a PowerShell interface to Sybase, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Sybase data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Sybase. To access Sybase data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Sybase.
Once you have acquired the necessary connection properties, accessing Sybase data in PowerShell can be enabled in three steps.
To connect to Sybase, specify the following connection properties:
Optionally, you can also secure your connections with TLS/SSL by setting UseSSL to true.
Sybase supports several methods for authentication including Password and Kerberos.
Set the AuthScheme to Password and set the following connection properties to use Sybase authentication.
To connect with LDAP authentication, configure Sybase server-side to use the LDAP authentication mechanism.
After configuring Sybase for LDAP, you can connect using the same credentials as Password authentication.
To leverage Kerberos authentication, begin by enabling it setting AuthScheme to Kerberos. See the Using Kerberos section in the Help documentation for more information on using Kerberos authentication.
You can find an example connection string below:
Server=MyServer;Port=MyPort;User=SampleUser;Password=SamplePassword;Database=MyDB;Kerberos=true;KerberosKDC=MyKDC;KerberosRealm=MYREALM.COM;KerberosSPN=server-name
Install the module:
Install-Module SybaseCmdlets
Connect:
$sybase = Connect-Sybase -User "$User" -Password "$Password" -Server "$Server" -Database "$Database" -Charset "$Charset"
Search for and retrieve data:
$productname = "Konbu" $products = Select-Sybase -Connection $sybase -Table "Products" -Where "ProductName = `'$ProductName`'" $products
You can also use the Invoke-Sybase cmdlet to execute SQL commands:
$products = Invoke-Sybase -Connection $sybase -Query 'SELECT * FROM Products WHERE ProductName = @ProductName' -Params @{'@ProductName'='Konbu'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Sybase\lib\System.Data.CData.Sybase.dll")
Connect to Sybase:
$conn= New-Object System.Data.CData.Sybase.SybaseConnection("User=myuser;Password=mypassword;Server=localhost;Database=mydatabase;Charset=iso_1;")
$conn.Open()
Instantiate the SybaseDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, ProductName from Products"
$da= New-Object System.Data.CData.Sybase.SybaseDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.id $_.productname
}
Update-Sybase -Connection $Sybase -Columns @('Id','ProductName') -Values @('MyId', 'MyProductName') -Table Products -Id "MyId"
$cmd = New-Object System.Data.CData.Sybase.SybaseCommand("UPDATE Products SET ProductName='Konbu' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Sybase.SybaseParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-Sybase -Connection $Sybase -Table Products -Columns @("Id", "ProductName") -Values @("MyId", "MyProductName")
$cmd = New-Object System.Data.CData.Sybase.SybaseCommand("INSERT INTO Products (ProductName) VALUES (@myProductName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Sybase.SybaseParameter("@myProductName","Konbu")))
$cmd.ExecuteNonQuery()
Remove-Sybase -Connection $Sybase -Table "Products" -Id "MyId"
$cmd = New-Object System.Data.CData.Sybase.SybaseCommand("DELETE FROM Products WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Sybase.SybaseParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Sybase Data Provider to get started:
Download NowLearn more:
👁 SAP Sybase IconRapidly create and deploy powerful .NET applications that integrate with Sybase databases.