Windows Azure(四-4):云上的轻量数据库--Table Storage

云端数据库

      Table Storage提供给我们一个云端的表格结构。我们可以把他想象为XML文件或者是一个轻量级的数据库

 

代码最能说明问题:

1. 准备
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace WT.Cloud.TableStorage
{
    internal class CustomerInfo : TableServiceEntity
    {
        public string CustomerID
        {

            get { return this.RowKey; }

            set { this.RowKey = value; }

        }

        public string CustomerName { get; set; }

        public int CustomerAge { get; set; }

        public CustomerInfo()
        {

            this.PartitionKey = "mypartitionkey";

        }

    }

    internal class CustomerInfoContext:TableServiceContext
    {
        public CustomerInfoContext(string baseAddress, StorageCredentials credentials) :
            base(baseAddress, credentials)
        {

        }

    }
}

 

2. 调用代码
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using System.Data.Services.Client;

namespace WT.Cloud.TableStorage
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            CloudTableClient tableStorage = storageAccount.CreateCloudTableClient();

            // 检查名为CustomerInfo的表格是否被创建,如果没有,创建它
            tableStorage.CreateTableIfNotExist("CustomerInfo");

            // 创建表格服务上下文
            CustomerInfoContext context = new CustomerInfoContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);

            // 插入两条客户信息数据,客户ID分别设置为0和1
            CustomerInfo ci1 = new CustomerInfo() { CustomerAge = 25, CustomerID = "0", CustomerName = "Mike" };
            CustomerInfo ci2 = new CustomerInfo() { CustomerAge = 32, CustomerID = "1", CustomerName = "Peter" };

            // 检查实体对象是否已经存在
            var existsQuery = from e
                    in context.CreateQuery<CustomerInfo>("CustomerInfo")
                              where
                              e.PartitionKey == ci1.PartitionKey
                              && e.RowKey == ci1.RowKey
                              select e;
            // 不做判断的话,可能出错:The specified resource does not exist.
            CustomerInfo existingObject = null;

            if (existsQuery != null)
            {
                existingObject = existsQuery.FirstOrDefault();
            }

            //  直接:context.AddObject("CustomerInfo", ci1);
            //  可能会出现错误:The specified entity already exists(实体对象已经存在)
            if (existingObject == null)
            {
                context.AddObject("CustomerInfo", ci1);
            }
            else
            {
                existingObject.CustomerID = ci1.CustomerID;
                existingObject.CustomerAge = ci1.CustomerAge;

                context.UpdateObject(existingObject);
            }

            // 保存(数据在这儿才真正的提交上去)
            context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);


            // 查询数据
            Console.WriteLine("Retrieve information of a customer whose ID is 0");

            var query = context.CreateQuery<CustomerInfo>("CustomerInfo").Where(c => c.CustomerID == "0").ToList();
            var returnedcustomerinfo = query.FirstOrDefault();

            Console.WriteLine(string.Format("Customer info retrieved: ID:{0},Name:{1},Age:{2}"
                               ,returnedcustomerinfo.CustomerID, returnedcustomerinfo.CustomerName, returnedcustomerinfo.CustomerAge));

            // 更新
            returnedcustomerinfo.CustomerAge = 100;
            context.UpdateObject(returnedcustomerinfo);
            Console.WriteLine("Updated, a customer whose ID is 0");

            // 重新查询
            Console.WriteLine("Retrieve information of a customer whose ID is 0");

            var query2 = context.CreateQuery<CustomerInfo>("CustomerInfo").Where(c => c.CustomerID == "0").ToList();
            var returnedcustomerinfo2 = query2.FirstOrDefault();

            Console.WriteLine(string.Format("Customer info retrieved: ID:{0},Name:{1},Age:{2}"
                               , returnedcustomerinfo2.CustomerID, returnedcustomerinfo2.CustomerName, returnedcustomerinfo2.CustomerAge));

            // 删除插入的两条客户数据
            // context.DeleteObject(ci1)-- 出错:The context is not currently tracking the entity
            // 说明要删除/更新一个数据必须先拿到它在云端的一个引用,不然就无法删除
            context.DeleteObject(returnedcustomerinfo2);
          

            context.SaveChanges();

            Console.WriteLine("The records has been deleted");
            Console.ReadLine();

        }
    }
}


 

问题:

1. the specified entity already exists" error in table storage。

    有朋友说,“'ve gotton that one before when I try to do an AddObject insteaad of and UpdateObject.  What is the scenario you are getting this message from?”,和我的情形类似。

   “As you've found, you can't just add another item that has the same row key and partition key, so you will need to run a query to check to see if the item already exists.  ”

 

 

其他资讯::关于将SqlServer,mySql数据导入Azure storeage  (csdn不让入链接,。。。)

 

 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值