本文介紹如何通過 .NET SDK 在Table Store的資料表中寫入單行資料。
前提條件
方法說明
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);