Automate Redis Integration Tasks from PowerShell
The CData ADO.NET Provider for Redis is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Redis.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Redis; this tutorial shows how to use the Provider to create, retrieve, update, and delete Redis data.
Once you have acquired the necessary connection properties, accessing Redis data in PowerShell can be enabled in three steps.
Set the following connection properties to connect to a Redis instance:
- Server: Set this to the name or address of the server your Redis instance is running on. You can specify the port in Port.
- Password: Set this to the password used to authenticate with a password-protected Redis instance , using the Redis AUTH command.
Set UseSSL to negotiate SSL/TLS encryption when you connect.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Redis\lib\System.Data.CData.Redis.dll") -
Connect to Redis:
$conn= New-Object System.Data.CData.Redis.RedisConnection("Server=127.0.0.1;Port=6379;Password=myPassword;") $conn.Open() -
Instantiate the RedisDataAdapter, execute an SQL query, and output the results:
$sql="SELECT City, CompanyName from Customers" $da= New-Object System.Data.CData.Redis.RedisDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.city $_.companyname }
Update Redis Data
$cmd = New-Object System.Data.CData.Redis.RedisCommand("UPDATE Customers SET Country='US' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Redis.RedisParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Redis Data
$cmd = New-Object System.Data.CData.Redis.RedisCommand("INSERT INTO Customers (Country) VALUES (@myCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Redis.RedisParameter("@myCountry","US")))
$cmd.ExecuteNonQuery()
Delete Redis Data
$cmd = New-Object System.Data.CData.Redis.RedisCommand("DELETE FROM Customers WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Redis.RedisParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject