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

问题描述 投票:0回答:1

请提供一些关于使用OData发送http请求来查询azure表存储的例子(控制台应用程序)?

c# httpclient azure-table-storage
1个回答
2
投票

更新

你可以找到你的 sastoken 像我的照片一样,在门户网站上。

而且你还需要更新 x-ms-date(需要。指定请求的协调世界时(UTC)。)在 HttpHelper 文件。

更多详情,您可以下载 我的演示 代码在github( 你可以下载我的 HttpHelper 文件)。)

enter image description here

enter image description here

enter image description here

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

你可以像支持的文档一样使用LinQ查询。

enter image description here

    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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.