简体   繁体   English

控制台应用程序使用 azure 存储 tableapi

[英]Console app to use azure storage tableapi

Please provide some examples (console application) on sending http request to query an azure table storage using OData?请提供一些关于发送 http 请求以使用 OData 查询 azure 表存储的示例(控制台应用程序)?

UPDATE更新

You can find your sastoken at portal like my pic.您可以像我的图片一样在门户网站上找到您的sastoken

And u also need to update the value of x-ms-date ( Required. Specifies the Coordinated Universal Time (UTC) for the request. ) in HttpHelper file.并且您还需要更新HttpHelper文件中的x-ms-date的值(必需。指定请求的协调世界时 (UTC)。 )。

For more details, you can download my demo code in github( You can download my HttpHelper file).更多详细信息,您可以在 github 上下载我的演示代码(您可以下载我的HttpHelper文件)。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

using Microsoft.Azure.Cosmos.Table;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using static ODatafilter.HttpHelper;

namespace ODatafilter
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Azure Cosmos Table Samples");
            string baseurl = @"https://panshubeistorage.table.core.windows.net/";
            string tbname = "People";//Console.ReadLine();
            string sastoken = @"?sv=2019-10-10&ss=************";
            string filter = @"&$filter=PartitionKey%20eq%20'Smith'%20";
            baseurl = baseurl + tbname + "()" + sastoken+filter;
            HttpResponseData data = HttpHelper.GetForOData(baseurl);
            string responseData = data.Data.Replace(".","_");
            ODataResponse odata = JsonConvert.DeserializeObject<ODataResponse>(responseData);

            foreach (ODatavalue m in odata.value)
            {
                Console.WriteLine(m.PartitionKey + "    " + m.PhoneNumber + "    " + m.RowKey + "   " + m.Email);
            }
            Console.WriteLine("Press any key to exit");
            Console.Read();
        }
        public class ODataResponse
        {
            public string odata_metadata { get; set; }
            public List<ODatavalue> value { get; set; }
        }
        public class ODatavalue {
            public string odata_type { get; set; }
            public string odata_id { get; set; }
            public string odata_etag { get; set; }
            public string odata_editLink { get; set; }
            public string Timestamp { get; set; }
            public string PartitionKey { get; set; }
            public string RowKey { get; set; }
            public string Email { get; set; }
            public string PhoneNumber { get; set; }
        }
    }
}

PRIVIOUS私人的

You can use LinQ to query like the document like you supported.您可以使用 LinQ 像您支持的文档一样查询。

在此处输入图像描述

    static async Task Main(string[] args)
    {
        Console.WriteLine("Azure Cosmos Table Samples");
        Console.WriteLine("Query data by filter");

        CloudTable table = GetTable();

        Console.WriteLine("pls input PartitionKey:");
        string PartitionKey = Console.ReadLine();
        Console.WriteLine("pls input RowKey:");
        string RowKey = Console.ReadLine();
        //Query
        IQueryable<CustomerEntity> linqQuery = table.CreateQuery<CustomerEntity>()
        .Where(x => x.PartitionKey== PartitionKey && x.RowKey== RowKey)
        .Select(x => new CustomerEntity() { PartitionKey = x.PartitionKey, RowKey = x.RowKey, Email = x.Email, PhoneNumber= x.PhoneNumber });

        var list = linqQuery.ToList<CustomerEntity>();

        foreach (CustomerEntity m in list)
        {
            Console.WriteLine(m.PartitionKey+"    "+m.PhoneNumber+"    "+m.RowKey+"   "+m.Email);
        }
        Console.WriteLine();
        Console.WriteLine("Press any key to exit");
        Console.Read();
    }

    public static CloudTable GetTable() {
        CloudStorageAccount account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=***x=core.windows.net");
        CloudTableClient tableClient = account.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference("People");
        return table;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM