全部产品
Search
文档中心

表格存储:读取单行数据

更新时间:May 11, 2026

本文介绍如何通过 .NET SDK 读取表格存储数据表的单行数据。

注意事项

读取数据时需要提供包含自增主键列值在内的完整主键值。

前提条件

初始化Tablestore Client

方法说明

public GetRowResponse GetRow(GetRowRequest request)

异步方法:

public Task<GetRowResponse> GetRowAsync(GetRowRequest request)

GetRowRequest参数说明

名称

类型

说明

tableName(必选)

string

数据表名称。

primaryKey(必选)

PrimaryKey

主键信息,包括主键列名称和主键值。

  • 主键列数据类型包括 STRING、INTEGER 和 BINARY。

  • 主键个数和类型必须与数据表的主键保持一致。

maxVersion(可选)

int

最大版本数,默认值为1。

  • 最大版本数和版本范围不可同时设置。

  • 如果符合查询条件的数据版本数量超过设置的最大版本数,按从新到旧的顺序返回指定版本数量的数据。

timeRange(可选)

TimeRange

数据版本范围。

  • 最大版本数和版本范围不可同时设置。

  • 表格存储数据表的每个属性列可以有不同的数据版本,设置版本范围后,仅返回版本范围内的数据。

columnsToGet(可选)

HashSet<string>

指定读取的数据列,可以是主键列或属性列。

  • 不设置columnsToGet时,返回整行数据。

  • 设置columnsToGet时,如果读取的行数据不包含任何指定的数据列,将返回 null。

condition(可选)

IColumnCondition

过滤条件,详情请参见过滤器

  • 如果同时设置columnsToGet和condition,先按columnsToGet筛选符合条件的数据行,再按condition条件过滤数据。

示例代码

以下示例代码读取了主键值为 row1 的单行数据。

try
{
    // 构造主键
    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };

    // 调用 GetRow 方法读取行数据
    GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey);
    GetRowResponse getRowResponse = client.GetRow(getRowRequest);

    Console.WriteLine($"RequestId: {getRowResponse.RequestID}");
    Console.WriteLine($"Read CU Cost: {getRowResponse.ConsumedCapacityUnit.Read}");
    Console.WriteLine($"Write CU Cost: {getRowResponse.ConsumedCapacityUnit.Write}");
    Console.WriteLine($"Row Data: {getRowResponse.Row}");
}
catch (Exception ex)
{
    Console.WriteLine($"Get row failed, exception: {ex.Message}");
}
  • 设置读取的数据版本范围,结果只返回版本范围内的数据。

    TimeRange timeRange = new TimeRange
    {
        StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - 86400 * 1000,
        EndTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
    };
    
    // 调用 GetRow 方法读取行数据
    GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey, null, null, timeRange);
  • 指定读取的属性列。

    HashSet<string> columnsToGet = new HashSet<string> { "col2" };
    
    // 调用 GetRow 方法读取行数据
    GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey, columnsToGet);