OtsClientBeginBatchModifyData Method Aliyun Open Services SDK for .NET
开始把PutData和/或DeleteData的多次调用组合成一个调用的异步操作。

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

public IAsyncResult BeginBatchModifyData(
	string tableName,
	IEnumerable<RowChange> rowChanges,
	string transactionId,
	AsyncCallback callback,
	Object state
)

Parameters

tableName
Type: SystemString
表(Table)名,不能为视图(View)名。
rowChanges
Type: System.Collections.GenericIEnumerableRowChange
RowChange对象的枚举器。
transactionId
Type: SystemString
事务(Transaction)ID。
callback
Type: SystemAsyncCallback
AsyncCallback委托。
state
Type: SystemObject
此请求的状态对象。

Return Value

Type: IAsyncResult
引用该异步请求的IAsyncResult对象。

Implements

IOtsBeginBatchModifyData(String, IEnumerableRowChange, String, AsyncCallback, Object)
Exceptions

ExceptionCondition
ArgumentException

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

- 或 -

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

- 或 -

rowChanges包含的信息无效。

ArgumentNullExceptionrowChanges为空引用。
Examples

下面的示例代码演示如何开始一个事务(Transaction),并异步地批量提交数据修改。该示例中使用CreateTable(TableMeta)方法示例代码所创建的表。
using System;
using System.Linq;
using System.Net;
using Aliyun.OpenServices.OpenTableService;

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

        public AsyncBatchModifyDataSample()
        {
            otsClient = new OtsClient(endpoint, accessId, accessKey);
        }

        public void BatchInsertData(string tableName)
        {
            int uid = 1;
            // 将5条RowPutChange放到列表中
            var rowChanges = new List<RowChange>();

            for (int i = 0; i < 5; i++)
            {
                var rowChange = new RowPutChange();
                rowChange.PrimaryKeys["uid"] = uid;
                rowChange.PrimaryKeys["flag"] = true;
                rowChange.PrimaryKeys["name"] = "contact " + i.ToString();

                rowChange.AttributeColumns["groupid"] = 1;
                rowChange.AttributeColumns["address"] = "中国某地";

                rowChanges.Add(rowChange);
            }

            // 开始一个事务
            try
            {
                var transactionId = otsClient.StartTransaction(tableName, uid);

                // 开始批量修改数据的异步操作
                otsClient.BeginBatchModifyData(tableName, rowChanges, transactionId, BatchModifyDataCallback, transactionId);

                // 这里进行其他操作
            }
            catch (OtsException ex)
            {
                Console.WriteLine("操作失败。OTS异常消息:" + ex.Message);
                // RequestId和HostId可以在有问题时用于联系客服诊断异常
                Console.WriteLine("Request ID: {0}\tHostID: {1}", ex.RequestId, ex.HostId);
            }
            catch (WebException ex)
            {
                Console.WriteLine("操作失败。网络异常:{0}。请检查Endpoint或网络链接。", ex.Message);
            }
        }

        private void BatchModifyDataCallback(IAsyncResult asyncResult)
        {
            try
            {
                var transactionId = asyncResult.AsyncState as string;

                // 操作结束时必须调用EndBatchModifyData方法,否则可能会产生资源泄露
                otsClient.EndBatchModifyData(asyncResult);

                // 需要调用ComitTransaction方法提交数据修改
                otsClient.CommitTransaction(transactionId);
            }
            catch (OtsException ex)
            {
                Console.WriteLine("操作失败。OTS异常消息:" + ex.Message);
                // RequestId和HostId可以在有问题时用于联系客服诊断异常
                Console.WriteLine("Request ID: {0}\tHostID: {1}", ex.RequestId, ex.HostId);
            }
            catch (WebException ex)
            {
                Console.WriteLine("操作失败。网络异常:{0}。请检查Endpoint或网络链接。", ex.Message);
            }
        }
    }
}
See Also

Reference