IOtsGetRow Method (SingleRowQueryCriteria)Aliyun Open Services SDK for .NET
获取表(Table)或视图(View)中的一行数据。

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

Row GetRow(
	SingleRowQueryCriteria criteria
)

Parameters

criteria
Type: Aliyun.OpenServices.OpenTableServiceSingleRowQueryCriteria
表示查询条件的SingleRowQueryCriteria对象。

Return Value

Type: Row
表示一行数据的Row的对象。如果指定的Row对象没找到,则返回null。
Exceptions

ExceptionCondition
ArgumentExceptioncriteria包含的信息无效。
ArgumentNullExceptioncriteria为空引用。
OtsException

OTS访问返回错误消息。

WebException

由于网络原因请求失败,

- 或 -

访问超时。

InvalidOperationException

返回结果解析错误。

Examples

下面的示例代码获取指定员工信息并根据基础工资和奖金比率计算奖金。示例中使用了GetRow(SingleRowQueryCriteria)方法,并进行了类型转换。
using System;
using System.Linq;
using Aliyun.OpenServices.OpenTableService;

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

        public void ComputeBonus(string tableName, int uid, string employee)
        {
            // 构造查询条件,假定数据的主键是uid和name
            var criteria = new SingleRowQueryCriteria(tableName);
            criteria.PrimaryKeys["uid"] = uid;
            criteria.PrimaryKeys["name"] = employee;
            // 指定返回列,假定rate列是比率,类型为Double;salary列为基本工资,类型为Integer
            criteria.ColumnNames.Add("name");
            criteria.ColumnNames.Add("rate");
            criteria.ColumnNames.Add("salary");

            var otsClient = new OtsClient(endpoint, accessId, accessKey);

            try
            {
                // 读取数据
                var row = otsClient.GetRow(criteria);
                if (row == null)
                {
                    Console.WriteLine("未找到员工{0}的信息。", employee);
                }

                try
                {
                    // 显式转换rate列的值为double类型,如果rate的ValueType != ColumnType.Double,则会抛出异常
                    double rate = (double)row.Columns["rate"];
                    // 显式转换rate列的值为long类型, 如果salary列的ValueType != ColumnType.Integer,则会抛出异常
                    long salary = (long)row.Columns["salary"];

                    var bonus = rate * salary;
                    Console.WriteLine("奖金数:{0}", bonus);
                }
                catch (InvalidCastException)
                {
                    Console.WriteLine("存储数据格式错误。");
                }
            }
            catch (OpenTableServiceException 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