本文介绍如何通过 .NET SDK 在表格存储的数据表中写入单行数据。
前提条件
方法说明
public PutRowResponse PutRow(PutRowRequest request)异步方法:
public Task<PutRowResponse> PutRowAsync(PutRowRequest request)示例代码
以下示例代码在 test_table 表中写入一行数据,该行数据的主键值为 row1。
try
{
// 构造主键
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// 写入行数据时必须配置写入条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
Condition condition = new Condition(RowExistenceExpectation.IGNORE);
// 调用 PutRow 方法写入行数据
PutRowRequest putRowRequest = new PutRowRequest("test_table", condition, primaryKey, null);
PutRowResponse putRowResponse = client.PutRow(putRowRequest);
Console.WriteLine($"* RequestId: {putRowResponse.RequestID}");
Console.WriteLine($"* Read CU Cost: {putRowResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"* Write CU Cost: {putRowResponse.ConsumedCapacityUnit.Write}");
}
catch (Exception ex)
{
Console.WriteLine($"Put row failed, exception: {ex.Message}");
}添加属性列。
AttributeColumns columns = new AttributeColumns { { "col1", new ColumnValue("val1") } }; // 构建 PutRowRequest PutRowRequest putRowRequest = new PutRowRequest("test_table", condition, primaryKey, columns);