Use the Tablestore .NET SDK to update a single row. You can modify attribute column values, add columns, or delete specific versions or entire attribute columns.
Prerequisites
Method
public UpdateRowResponse UpdateRow(UpdateRowRequest request)
Asynchronous method:
public Task<UpdateRowResponse> UpdateRowAsync(UpdateRowRequest request)
Sample code
This example updates the row with primary key row1, setting attribute column col1 to changed_val1.
try
{
// Construct the primary key.
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// Define the attribute columns to update.
UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute();
updateOfAttribute.AddAttributeColumnToPut("col1", new ColumnValue("changed_val1"));
// You must specify a condition when updating a row. (RowExistenceExpectation.IGNORE skips the row existence check.)
Condition condition = new Condition(RowExistenceExpectation.IGNORE);
// Call the UpdateRow method to update the row.
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}");
}
You can also perform these operations:
-
Add an attribute column.
updateOfAttribute.AddAttributeColumnToPut("col2", new ColumnValue("val2")); -
Delete an entire attribute column.
updateOfAttribute.AddAttributeColumnToDelete("col2");