Use Tablestore SDK for .NET to manage data in wide column tables.
Quick start
Get started with Tablestore SDK for .NET. Set up your environment, install the SDK, and initialize the client.
Prerequisites
The SDK is built on .NET Standard 2.0 and supports the following platforms and runtimes:
|
Platform |
Runtime |
|
Windows |
|
|
Linux and macOS |
|
Install the SDK
Install Tablestore SDK for .NET from NuGet or from source.
NuGet
Install with the .NET CLI:
dotnet add package Aliyun.TableStore.SDK
Alternatively, install from the NuGet Package Manager in Visual Studio:
-
Open or create a project in Visual Studio, and then choose Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
NoteIf NuGet is not installed in Visual Studio, download and install it from NuGet.
Search for aliyun.tablestore and select Aliyun.TableStore.SDK from the results.
Select the latest version and click Install. The SDK is automatically added to your project.
Source code
Import the SDK source code into your project.
-
Clone the SDK repository from GitHub:
git clone https://github.com/aliyun/aliyun-tablestore-csharp-sdk.gitNoteIf Git is not installed, download and install it from Git.
In Visual Studio, right-click Solution and select Add > Existing Project.
In the dialog box, select the aliyun-tablestore-sdk.csproj file and click Open.
Right-click Project and select References > Add Reference. In the dialog box, switch to the Projects tab and select aliyun-tablestore-sdk.
Click OK.
Configure access credentials
Create an AccessKey for your Alibaba Cloud account or a RAM user. Then, configure the AccessKey in the environment variables as shown below. Configuring the AccessKey in environment variables improves security because it prevents you from hard-coding sensitive information in your code.
After the configuration is complete, you must restart or refresh your development environment. This includes your IDE, command-line interface, other desktop applications, and background services. This ensures that the latest system environment variables are loaded. For more information about other types of access credentials, see Configure access credentials.
Linux
-
Run the following commands to append the environment variable settings to the
~/.bashrcfile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc -
Run the following command to apply the changes.
source ~/.bashrc -
Run the following commands to verify that the environment variables are effective.
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
-
Run the following command in the terminal to view your default shell type.
echo $SHELL -
Perform the following operations based on your default shell type.
Zsh
-
Run the following commands to append the environment variable settings to the
~/.zshrcfile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc -
Run the following command to apply the changes.
source ~/.zshrc -
Run the following commands to verify that the environment variables are effective.
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
-
Run the following commands to append the environment variable settings to the
~/.bash_profilefile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile -
Run the following command to apply the changes.
source ~/.bash_profile -
Run the following commands to verify that the environment variables are effective.
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
-
Windows
CMD
-
Run the following commands in Command Prompt (CMD) to set the environment variables.
setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET" -
Restart CMD and run the following commands to verify that the environment variables are effective.
echo %TABLESTORE_ACCESS_KEY_ID% echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
-
Run the following commands in PowerShell.
[Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User) -
Run the following commands to verify that the environment variables are effective.
[Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
Initialize the client
Tablestore SDK for .NET is thread-safe. When you use multiple threads, share a single OTSClient instance. After you initialize the client, list all tables in the instance to verify the connection.
Public network access is disabled by default for new instances. To access instance resources over the public network, enable public access in Network Management.
using System;
using Aliyun.OTS;
using Aliyun.OTS.Request;
using Aliyun.OTS.Response;
namespace Aliyun.OTS.Samples
{
public class Sample
{
public static void InitializeClient()
{
// Replace yourEndpoint with the instance endpoint
string endpoint = "yourEndpoint";
// Replace yourInstanceName with the instance name
string instanceName = "yourInstanceName";
// Obtain the AccessKey ID and AccessKey Secret from environment variables
string accessKeyId = Environment.GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID");
string accessKeySecret = Environment.GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET");
OTSClientConfig config = new OTSClientConfig(endpoint, accessKeyId, accessKeySecret, instanceName)
{
OTSDebugLogHandler = null,
OTSErrorLogHandler = null
};
try
{
// Initialize the Tablestore client
OTSClient client = new OTSClient(config);
// List and print all tables in the instance
ListTableResponse response = client.ListTable(new ListTableRequest());
foreach (var tableName in response.TableNames)
{
Console.WriteLine(tableName);
}
}
catch (Exception ex)
{
Console.WriteLine("List table failed, exception:{0}", ex.Message);
}
}
}
}
Version compatibility
The latest SDK version is 6.x.x. Compatibility with earlier versions:
|
Version |
Compatibility |
Description |
|
5.x.x |
Compatible |
N/A |
|
4.x.x |
Compatible |
N/A |
|
3.x.x |
Compatible |
N/A |
|
2.x.x |
Incompatible |
The following interfaces are removed: |
For detailed version history, see Version history of Tablestore SDK for .NET.
FAQ
Common issues with Tablestore SDK for .NET:
References
For information about error handling in Tablestore, see Error handling.