🔷 Read Data
- PartitionKey와 RowKey를 비교하여 데이터를 가져옴
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using MRTK.Tutorials.AzureCloudServices.Scripts.Domain;
using UnityEngine;
using UnityEngine.Events;
namespace MRTK.Tutorials.AzureCloudServices.Scripts.Managers
{
public class DataManager : MonoBehaviour
{
public bool IsReady { get; private set; }
[Header("Base Settings")]
[SerializeField]
private string connectionString = default;
[Header("Table Settings")]
[SerializeField]
private string projectsTableName = "students";
[SerializeField]
private bool tryCreateTableOnStart = true;
[Header("Events")]
[SerializeField]
private UnityEvent onDataManagerReady = default;
[SerializeField]
private UnityEvent onDataManagerInitFailed = default;
private CloudStorageAccount storageAccount;
private CloudTableClient cloudTableClient;
private CloudTable projectsTable;
private async void Awake()
{
storageAccount = CloudStorageAccount.Parse(connectionString);
cloudTableClient = storageAccount.CreateCloudTableClient();
projectsTable = cloudTableClient.GetTableReference(projectsTableName);
if (tryCreateTableOnStart)
{
try
{
if (await projectsTable.CreateIfNotExistsAsync())
{
Debug.Log($"Created table {projectsTableName}.");
}
}
catch (StorageException ex)
{
Debug.LogError("Failed to connect with Azure Storage.\nIf you are running with the default storage emulator configuration, please make sure you have started the storage emulator.");
Debug.LogException(ex);
onDataManagerInitFailed?.Invoke();
}
}
InsertData();
ReadData();
IsReady = true;
onDataManagerReady?.Invoke();
}
public void InsertData()
{
TableOperation operation;
TableResult result;
Student student;
student = new Student
{
PartitionKey = "00",
RowKey = "00",
Name = "OJUI",
Email = "ojui@example.com",
Address = "Developer Village"
};
operation = TableOperation.InsertOrMerge(student);
result = projectsTable.ExecuteAsync(operation).Result;
}
public async void ReadData()
{
CloudTable table = projectsTable;
string partitionKey = "02";
string rowKey = "02";
TableOperation retrieveOperation = TableOperation.Retrieve<Student>(partitionKey, rowKey);
TableResult result = await table.ExecuteAsync(retrieveOperation);
Student student = result.Result as Student;
if (student != null)
{
Debug.Log($"----- READ FUNCTION -----");
Debug.Log($"이름 : {student.Name}");
Debug.Log($"주소 : {student.Address}");
}
}
}
public class Student : TableEntity
{
public Student(string PartitionKey, string RowKey, string Name, string Email = null, string Address = null)
{
this.PartitionKey = PartitionKey;
this.RowKey = RowKey;
this.Name = Name;
this.Email = Email;
this.Address = Address;
}
public Student() { }
public string Name { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
}
- ReadData() 추가
- play
- 데이터를 잘 읽어오는 것을 확인
'Unity > Database' 카테고리의 다른 글
[유니티 Json 파싱] Newtonsoft.Json & Unity JSON Utility (0) | 2022.05.26 |
---|---|
04. Azure CosmosDB - Delete (0) | 2021.09.06 |
02. Azure CosmosDB - Insert (0) | 2021.08.28 |
01. Azure CosmosDB Settings (0) | 2021.08.21 |
00. Azure 체험계정 활성화 (0) | 2021.08.20 |