OtsClientPutData Method (String, RowPutChange)Aliyun Open Services SDK for .NET
插入一行或修改指定行中的数据。

Namespace: Aliyun.OpenServices.OpenTableService
Assembly: Aliyun.OpenServices (in Aliyun.OpenServices.dll) Version: 1.0.5290.21916
Syntax

public void PutData(
	string tableName,
	RowPutChange rowChange
)

Parameters

tableName
Type: SystemString
表(Table)名,不能为视图(View)名。
rowChange
Type: Aliyun.OpenServices.OpenTableServiceRowPutChange
包含数据的RowPutChange对象。

Implements

IOtsPutData(String, RowPutChange)
Exceptions

ExceptionCondition
ArgumentException

tableName为空引用或值为空字符串,

- 或 -

talbeName违反OTS名称的命名规则。

- 或 -

rowChange包含的信息无效。

ArgumentNullExceptionrow为空引用。
OtsException

OTS访问返回错误消息。

WebException

由于网络原因请求失败,

- 或 -

访问超时。

InvalidOperationException

返回结果解析错误。

Examples

下面的示例代码演示如何通过OtsClient的方法插入一条数据。
using System;
using System.Linq;
using Aliyun.OpenServices.OpenTableService;

namespace Aliyun.OpenServices.Samples.OpenTableService
{
    class PutDataSample
    {
        string endpoint = "http://ots.aliyuncs.com";
        string accessId = "<your access id>";
        string accessKey = "<your access key>";

        public void PutData(string tableName)
        {
            // 构造RowPutChange
            var rowChange = new RowPutChange();
            // 注意rowChange的主键信息必须与创建表时指定的主键个数、名称及类型均一致
            // 可以直接赋值主键为支持的类型对象,包括整型、布尔型和字符串。
            rowChange.PrimaryKeys["uid"] = 1;
            rowChange.PrimaryKeys["flag"] = true;
            rowChange.PrimaryKeys["name"] = "张三";
            //.其他属性信息放在AttributeColumns中,可以是建表时没有指定的列
            // 可以直接赋值列值为支持的类型对象,包括整型、浮点型、布尔型和字符串。
            rowChange.AttributeColumns["groupid"] = 1;
            rowChange.AttributeColumns["mobile"] = "11111111111";
            rowChange.AttributeColumns["address"] = "中国某地";
            rowChange.AttributeColumns["age"] = 20;

            // 提交数据
            var otsClient = new OtsClient(endpoint, accessId, accessKey);

            try
            {
                otsClient.PutData(tableName, rowChange);
            }
            catch (OtsException ex)
            {
                Console.WriteLine("插入数据失败。OTS异常消息: " + ex.Message);
                Console.WriteLine("Request ID: {0}\tHostID: {1}", ex.RequestId, ex.HostId);
            }
            catch (System.Net.WebException ex)
            {
                Console.WriteLine("创建表失败。网络异常:{0}。请检查Endpoint或网络链接。", ex.Message);
            }
        }
    }
}
See Also

Reference