.NET SDK

Updated at:

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

  • .NET Core 2.0 or later, or .NET Framework 4.0 or later

  • Visual Studio 2010 or later

Linux and macOS

  • .NET Core 2.0 or later

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:

  1. Open or create a project in Visual Studio, and then choose Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

    Note

    If NuGet is not installed in Visual Studio, download and install it from NuGet.

  2. Search for aliyun.tablestore and select Aliyun.TableStore.SDK from the results.

  3. 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.

  1. Clone the SDK repository from GitHub:

    git clone https://github.com/aliyun/aliyun-tablestore-csharp-sdk.git
    Note

    If Git is not installed, download and install it from Git.

  2. In Visual Studio, right-click Solution and select Add > Existing Project.

  3. In the dialog box, select the aliyun-tablestore-sdk.csproj file and click Open.

  4. Right-click Project and select References > Add Reference. In the dialog box, switch to the Projects tab and select aliyun-tablestore-sdk.

  5. 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

  1. 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
  2. Apply the changes:

    source ~/.bashrc
  3. Verify the environment variables:

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. Check your default shell:

    echo $SHELL
  2. Configure based on your shell type:

    Zsh
    1. 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
    2. Apply the changes:

      source ~/.zshrc
    3. Verify the environment variables:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. 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
    2. Apply the changes:

      source ~/.bash_profile
    3. Verify the environment variables:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. Set the environment variables in CMD:

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. Restart CMD and verify:

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. 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)
  2. 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.

Important

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: Condition.IGNORE, Condition.EXPECT_EXIST, and Condition.EXPECT_NOT_EXIST. The DLL file is renamed from Aliyun.dll to Aliyun.TableStore.dll.

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.