PrimaryKeyValue StructureAliyun Open Services SDK for .NET
表示主键(PrimaryKey)列的值。

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

public struct PrimaryKeyValue

The PrimaryKeyValue type exposes the following members.

Methods

  NameDescription
Public methodEquals
比较是否与另一实例相等。
(Overrides ValueTypeEquals(Object).)
Public methodGetHashCode
获取对象的哈希值。
(Overrides ValueTypeGetHashCode.)
Public methodGetType
获取当前实例的 Type
(Inherited from Object.)
Public methodToString
获取值的字符串表示。
(Overrides ValueTypeToString.)
Top
Operators

  NameDescription
Public operatorStatic memberEquality
相等操作符。
Public operatorStatic member(PrimaryKeyValue to String)
显式转换操作符,将PrimaryKeyValue对象转换为String对象。
Public operatorStatic member(PrimaryKeyValue to Int64)
显式转换操作符,将PrimaryKeyValue对象转换为Int64对象。
Public operatorStatic member(PrimaryKeyValue to Boolean)
显式转换操作符,将PrimaryKeyValue对象转换为Boolean对象。
Public operatorStatic member(Boolean to PrimaryKeyValue)
隐式转换操作符,将Boolean对象转换为PrimaryKeyValue对象。
Public operatorStatic member(Int64 to PrimaryKeyValue)
隐式转换操作符,将Int64对象转换为PrimaryKeyValue对象。
Public operatorStatic member(String to PrimaryKeyValue)
隐式转换操作符,将String对象转换为PrimaryKeyValue对象。 主键值不能为空引用或者长度为0的字符串。
Public operatorStatic memberInequality
不相等操作符。
Top
Properties

  NameDescription
Public propertyValueType
获取值的数据类型。
Top
Remarks

此类型对象可以直接与StringInt64Boolean类型 相互转换。

可以将StringInt64Boolean对象隐式地转换为PrimaryKeyValue类型的对象, 转换后的对象的ValueType属性为相对应的PrimaryKeyType枚举的值。 例如,将一个Int64对象转换为PrimaryKeyValue,则转换后对象的ValueType等于Integer

可以将PrimaryKeyValue显式地转换为StringInt64Boolean。 但需要根据ValueType指定的类型进行转换,否则会抛出InvalidCastException的异常。

调用ToString方法可以得到值的字符串表示形式。

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