本文介绍如何通过 .NET SDK 读取表格存储数据表的单行数据。
注意事项
读取数据时需要提供包含自增主键列值在内的完整主键值。
前提条件
方法说明
public GetRowResponse GetRow(GetRowRequest request)异步方法:
public Task<GetRowResponse> GetRowAsync(GetRowRequest request)示例代码
以下示例代码读取了主键值为 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);