.NET SDK
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 RAM user, then configure it as an environment variable to avoid hard-coding credentials.
Restart your IDE, terminal, other desktop applications, and background services after configuration to load the updated environment variables. For more information about other types of access credentials, see Configure access credentials.
Linux
-
Append the environment variables to
~/.bashrc:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc -
Apply the changes:
source ~/.bashrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
-
Check your default shell:
echo $SHELL -
Configure based on your shell type:
Zsh
-
Append the environment variables to
~/.zshrc:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc -
Apply the changes:
source ~/.zshrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
-
Append the environment variables to
~/.bash_profile:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile -
Apply the changes:
source ~/.bash_profile -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
-
Windows
CMD
-
Set the environment variables in CMD:
setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET" -
Restart CMD and verify:
echo %TABLESTORE_ACCESS_KEY_ID% echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
-
Run 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) -
Verify the environment variables:
[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 .NET SDK error handling.