本文介紹如何通過 .NET SDK 更新Table Store資料表中的單行資料,您可以更新屬性列的值、添加屬性列、刪除屬性列的某個版本或整個屬性列。
前提條件
方法說明
public UpdateRowResponse UpdateRow(UpdateRowRequest request)非同步方法呼叫:
public Task<UpdateRowResponse> UpdateRowAsync(UpdateRowRequest request)範例程式碼
以下範例程式碼用於修改 test_table 表中主索引值為 row1 的行資料,將屬性列 col1 的值修改為 changed_val1。
try
{
// 構造主鍵
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// 更新的屬性列
UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute();
updateOfAttribute.AddAttributeColumnToPut("col1", new ColumnValue("changed_val1"));
// 更新行資料時必須指定更新條件 (RowExistenceExpectation.IGNORE,表示不做行存在性判斷)
Condition condition = new Condition(RowExistenceExpectation.IGNORE);
// 調用 UpdateRow 方法更新行資料
UpdateRowRequest updateRowRequest = new UpdateRowRequest("test_table", condition, primaryKey, updateOfAttribute);
UpdateRowResponse updateRowResponse = client.UpdateRow(updateRowRequest);
Console.WriteLine($"RequestId: {updateRowResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {updateRowResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {updateRowResponse.ConsumedCapacityUnit.Write}");
}
catch (Exception ex)
{
Console.WriteLine($"Update row failed, exception: {ex.Message}");
}您也可以參照範例程式碼進行以下行資料操作。
添加一個屬性列。
updateOfAttribute.AddAttributeColumnToPut("col2", new ColumnValue("val2"));刪除整個屬性列資料。
updateOfAttribute.AddAttributeColumnToDelete("col2");