全部產品
Search
文件中心

Tablestore:寫入單行資料

更新時間:May 12, 2026

本文介紹如何通過 .NET SDK 在Table Store的資料表中寫入單行資料。

前提條件

初始化Tablestore Client

方法說明

public PutRowResponse PutRow(PutRowRequest request)

非同步方法呼叫:

public Task<PutRowResponse> PutRowAsync(PutRowRequest request)

PutRowRequest參數說明

名稱

類型

說明

tableName(必選)

string

資料表名稱。

primaryKey(必選)

PrimaryKey

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

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

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

columns(可選)

AttributeColumns

屬性列資訊,包括屬性列名稱、屬性列值和資料版本號碼。

  • 屬性列資料類型包括 STRING、INTEGER、BINARY、DOUBLE 和 BOOLEAN。

  • 資料版本號碼即時間戳記,預設由系統自動產生,也可以自己指定,詳情請參見資料版本和生命週期

condition(必選)

Condition

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

範例程式碼

以下範例程式碼在 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);

相關文檔

批次更新資料