iT邦幫忙

2022 iThome 鐵人賽

DAY 21
0
IT管理

Azure開發者必備掌握的基本系列 第 21

Azure開發者必備掌握的基本_第21天_Azure Table Storage_操作與C# API常見操作

  • 分享至 

  • xImage
  •  

至Azure Storage Account下特定容器後
移動至Tables
去做新增並指定Table name
https://ithelp.ithome.com.tw/upload/images/20221002/20107452pURPbru5Qf.png

移至
儲存體瀏覽器 (預覽)
我們還能查看剛的資料
https://ithelp.ithome.com.tw/upload/images/20221002/201074522N4eCjpiH8.png

在更深入一層Portal操作點進去看
https://ithelp.ithome.com.tw/upload/images/20221002/20107452AOGG8nGAAU.png

可以進行"新增實體"
https://ithelp.ithome.com.tw/upload/images/20221002/20107452PsAqOVozmT.png

https://ithelp.ithome.com.tw/upload/images/20221002/20107452jc9mrp0JB6.png

再新增一個 多一個不同的property
https://ithelp.ithome.com.tw/upload/images/20221002/20107452rSdP5edtBQ.png

所以可看到schema可動態新增
也不被侷限schema pre-defined
https://ithelp.ithome.com.tw/upload/images/20221002/20107452yAFOYXcJQ3.png

要刪除也可以複選操作
https://ithelp.ithome.com.tw/upload/images/20221002/20107452fqzRFC508w.png

在此進行C# API端操作
添加
https://ithelp.ithome.com.tw/upload/images/20221002/20107452Nh2QlEOoHJ.png

nuget安裝
Azure.Data.Tables
https://ithelp.ithome.com.tw/upload/images/20221002/20107452vB5K6fDD6J.png

connection string 也是從access key去複製過來的(比照blob前幾篇介紹)

using Azure.Data.Tables;

string connStr = ".....core.windows.net";
string tableName = "Orders";

AddEntity("01", "Mobile", 100);
AddEntity("02", "Laptop", 80);
AddEntity("03", "Desktop", 90);
AddEntity("04", "Laptop", 200);


void AddEntity(string orderID, string category, int quantity)
{
    TableClient tableClient = new TableClient(connStr, tableName);
    TableEntity entity = new TableEntity(category, orderID)
    {
        {"quantity", quantity}
    };
    tableClient.AddEntity(entity);
    Console.WriteLine("Added Entity with order ID :{0}", orderID);
}

https://ithelp.ithome.com.tw/upload/images/20221002/201074524JHow8rAHZ.png

讀取
https://ithelp.ithome.com.tw/upload/images/20221003/20107452PQM1OgUwxy.png

using Azure;
using Azure.Data.Tables;

string connStr = "DefaultEndpointsProtocol=https;AccountName=sastg1;AccountKey=KUe+phizs0k+5fzT6KhhtL5nVOHCKIAUtXe+jsJurGzoB+Qka8xHxOHf4lhFjjKOM79drnFFsQLx+AStAm8Yeg==;EndpointSuffix=core.windows.net";
string tableName = "Orders";

//AddEntity("01", "Mobile", 100);
//AddEntity("02", "Laptop", 80);
//AddEntity("03", "Desktop", 90);
//AddEntity("04", "Laptop", 200);
QueryEntity("Laptop");

void AddEntity(string orderID, string category, int quantity)
{
    TableClient tableClient = new TableClient(connStr, tableName);
    TableEntity entity = new TableEntity(category, orderID)
    {
        {"quantity", quantity}
    };
    tableClient.AddEntity(entity);
    Console.WriteLine("Added Entity with order ID :{0}", orderID);
}

void QueryEntity(string category)
{
    TableClient tableClient = new TableClient(connStr, tableName);
    Pageable<TableEntity> results = tableClient.Query<TableEntity>(entity => entity.PartitionKey == category);
    foreach(TableEntity tableEntity in results)
    {
        Console.WriteLine("Order Id : {0}", tableEntity.RowKey);
        Console.WriteLine("Quantity : {0}",tableEntity.GetInt32("quantity"));
    }
}



刪除
https://ithelp.ithome.com.tw/upload/images/20221003/20107452Of1LaPTGlg.png

using Azure;
using Azure.Data.Tables;

string connStr = "DefaultEndpointsProtocol=https;AccountName=sastg1;AccountKey=KUe+phizs0k+5fzT6KhhtL5nVOHCKIAUtXe+jsJurGzoB+Qka8xHxOHf4lhFjjKOM79drnFFsQLx+AStAm8Yeg==;EndpointSuffix=core.windows.net";
string tableName = "Orders";

//AddEntity("01", "Mobile", 100);
//AddEntity("02", "Laptop", 80);
//AddEntity("03", "Desktop", 90);
//AddEntity("04", "Laptop", 200);
Console.WriteLine("before Delete");
QueryEntity("Laptop");
Console.WriteLine("after Delete");
DeleteEntity("Laptop", "04");
QueryEntity("Laptop");

void AddEntity(string orderID, string category, int quantity)
{
    TableClient tableClient = new TableClient(connStr, tableName);
    TableEntity entity = new TableEntity(category, orderID)
    {
        {"quantity", quantity}
    };
    tableClient.AddEntity(entity);
    Console.WriteLine("Added Entity with order ID :{0}", orderID);
}

void QueryEntity(string category)
{
    TableClient tableClient = new TableClient(connStr, tableName);
    Pageable<TableEntity> results = tableClient.Query<TableEntity>(entity => entity.PartitionKey == category);
    foreach(TableEntity tableEntity in results)
    {
        Console.WriteLine("Order Id : {0}", tableEntity.RowKey);
        Console.WriteLine("Quantity : {0}",tableEntity.GetInt32("quantity"));
    }
}

void DeleteEntity(string category,string orderID)
{
    TableClient tableClient = new TableClient(connStr, tableName);
    tableClient.DeleteEntity(category, orderID);
    Console.WriteLine("entity is deleted!!");
}




上一篇
Azure開發者必備掌握的基本_第20天_Azure Table Storage
下一篇
Azure開發者必備掌握的基本_第22天_Azure Cosmos DB
系列文
Azure開發者必備掌握的基本30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言