全部產品
Search
文件中心

Tablestore:更新單行資料

更新時間:May 12, 2026

本文介紹如何通過 .NET SDK 更新Table Store資料表中的單行資料,您可以更新屬性列的值、添加屬性列、刪除屬性列的某個版本或整個屬性列。

前提條件

初始化Tablestore Client

方法說明

public UpdateRowResponse UpdateRow(UpdateRowRequest request)

非同步方法呼叫:

public Task<UpdateRowResponse> UpdateRowAsync(UpdateRowRequest request)

UpdateRowRequest參數說明

名稱

類型

說明

tableName(必選)

string

資料表名稱。

primaryKey(必選)

PrimaryKey

主鍵資訊,包括主鍵列名稱和主索引值。

  • 主鍵列資料類型包括 STRING、INTEGER 和 BINARY。

  • 主鍵個數和類型必須與資料表的主鍵保持一致。

updateOfAttribute(必選)

UpdateOfAttribute

更新的屬性列資訊和操作類型。

condition(必選)

Condition

更新條件,詳情請參見條件更新

範例程式碼

以下範例程式碼用於修改 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");

相關文檔