Azure table 对 csv c# 的查询
Azure table query to csv c#
我正在尝试将 Azure table 存储查询结果写入 .csv 文件并将其本地存储在计算机上(临时就可以)。我能够毫无问题地查询 - 然后在消息框中显示结果。我正在使用 c# 执行此操作。我不想使用外部应用程序,但如果需要的话,我会调用 powershell 脚本。最终,我尝试下载 csv,因此我查询 csv 文件而不是 azure tables 存储以获得更多功能。 (SQL 目前不能选择服务器 - 虽然我知道它会让我的生活变得更轻松)
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse ("DefaultEndpointsProtocol=https;AccountName=MyStorageAccountNameHere;AccountKey=MyTableKeyhere
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TelephonyIssueLog");
var managerQuery = new TableQuery<IssueEntity>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "Issues"));
System.IO.File.WriteAllText(@"C:\temp\csv.csv", managerQuery);
我真的想通了:
从顶部开始
using CsvHelper;
if (string.IsNullOrEmpty(txtFirstN.Text) || string.IsNullOrEmpty(txtLName.Text) || string.IsNullOrEmpty(cbDirection.Text) || string.IsNullOrEmpty(cbPhoneSystem.Text) || string.IsNullOrEmpty(txtCustPhone.Text) || string.IsNullOrEmpty(txtManager.Text) || string.IsNullOrEmpty(txtProgram.Text) || string.IsNullOrEmpty(cbLocation.Text) || string.IsNullOrEmpty(txtPhoneNumber.Text) || string.IsNullOrEmpty(cbIssue.Text) || string.IsNullOrEmpty(cbPhoneSystem.Text))
{
MessageBox.Show("Please Fill out the WHOLE Form. Thank you!");
}
else
{
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=myaccountname;AccountKey=my-account-key
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TelephonyIssueLog");
//Await is a good command to use here so we don't try to go forward before we verify the table actually exists and error out.
//Notice I made the function async. C# is annoying as all out when using async and await so yeah have fun with that :D. -=Chris
await table.CreateIfNotExistsAsync();
IssueEntity IssueLog = new IssueEntity("Issues", lblRandom.Text);
IssueLog.FirstName = txtFirstN.Text;
IssueLog.LastName = txtLName.Text;
IssueLog.CallDirection = cbDirection.Text;
IssueLog.CustNumber = txtCustPhone.Text;
//IssueLog.FirstName = tbFirstName.Text;
//IssueLog.LastName = tbLastName.Text;
IssueLog.Location = cbLocation.Text;
IssueLog.Manager = txtManager.Text;
IssueLog.PhoneNumber = txtPhoneNumber.Text;
IssueLog.PhoneSystem = cbPhoneSystem.Text;
IssueLog.PrimaryIssue = cbIssue.Text;
IssueLog.Program = txtProgram.Text;
TableOperation insertOperation = TableOperation.Insert(IssueLog);
table.Execute(insertOperation);
然后是查询和 CSV 编辑:
//get to the cloud storage
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=my-account-name;AccountKey=My-Account-Key
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TelephonyIssueLog");
工作得很好!
//initiate the writer
var sw = new StreamWriter(@"C:\ProgramData\N3RASupportNotifier\Test.csv");
var writer = new CsvWriter(sw);
TableQuery<IssueEntity> query = new TableQuery<IssueEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Issues"));
//Write each record to CSV
foreach (IssueEntity entity in table.ExecuteQuery(query))
{
writer.WriteField(entity.FirstName);
writer.WriteField(entity.LastName);
writer.WriteField(entity.Location);
writer.WriteField(entity.Manager);
writer.WriteField(entity.PartitionKey);
writer.WriteField(entity.PhoneSystem);
writer.WriteField(entity.PrimaryIssue);
writer.WriteField(entity.Timestamp);
writer.NextRecord();
}
为了简单起见,我建议您使用名为 ServiceStack.Text 的第 3 方库来实现您的目的。从 Azure Table 筛选数据后,您可以尝试添加以下代码:
string csvString = CsvSerializer.SerializeToCsv<IssueEntity>(managerQuery);
System.IO.File.WriteAllText(@"C:\temp\csv.csv", csvString);
我正在尝试将 Azure table 存储查询结果写入 .csv 文件并将其本地存储在计算机上(临时就可以)。我能够毫无问题地查询 - 然后在消息框中显示结果。我正在使用 c# 执行此操作。我不想使用外部应用程序,但如果需要的话,我会调用 powershell 脚本。最终,我尝试下载 csv,因此我查询 csv 文件而不是 azure tables 存储以获得更多功能。 (SQL 目前不能选择服务器 - 虽然我知道它会让我的生活变得更轻松)
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse ("DefaultEndpointsProtocol=https;AccountName=MyStorageAccountNameHere;AccountKey=MyTableKeyhere
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TelephonyIssueLog");
var managerQuery = new TableQuery<IssueEntity>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "Issues"));
System.IO.File.WriteAllText(@"C:\temp\csv.csv", managerQuery);
我真的想通了: 从顶部开始
using CsvHelper;
if (string.IsNullOrEmpty(txtFirstN.Text) || string.IsNullOrEmpty(txtLName.Text) || string.IsNullOrEmpty(cbDirection.Text) || string.IsNullOrEmpty(cbPhoneSystem.Text) || string.IsNullOrEmpty(txtCustPhone.Text) || string.IsNullOrEmpty(txtManager.Text) || string.IsNullOrEmpty(txtProgram.Text) || string.IsNullOrEmpty(cbLocation.Text) || string.IsNullOrEmpty(txtPhoneNumber.Text) || string.IsNullOrEmpty(cbIssue.Text) || string.IsNullOrEmpty(cbPhoneSystem.Text))
{
MessageBox.Show("Please Fill out the WHOLE Form. Thank you!");
}
else
{
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=myaccountname;AccountKey=my-account-key
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TelephonyIssueLog");
//Await is a good command to use here so we don't try to go forward before we verify the table actually exists and error out.
//Notice I made the function async. C# is annoying as all out when using async and await so yeah have fun with that :D. -=Chris
await table.CreateIfNotExistsAsync();
IssueEntity IssueLog = new IssueEntity("Issues", lblRandom.Text);
IssueLog.FirstName = txtFirstN.Text;
IssueLog.LastName = txtLName.Text;
IssueLog.CallDirection = cbDirection.Text;
IssueLog.CustNumber = txtCustPhone.Text;
//IssueLog.FirstName = tbFirstName.Text;
//IssueLog.LastName = tbLastName.Text;
IssueLog.Location = cbLocation.Text;
IssueLog.Manager = txtManager.Text;
IssueLog.PhoneNumber = txtPhoneNumber.Text;
IssueLog.PhoneSystem = cbPhoneSystem.Text;
IssueLog.PrimaryIssue = cbIssue.Text;
IssueLog.Program = txtProgram.Text;
TableOperation insertOperation = TableOperation.Insert(IssueLog);
table.Execute(insertOperation);
然后是查询和 CSV 编辑:
//get to the cloud storage
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=my-account-name;AccountKey=My-Account-Key
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TelephonyIssueLog");
工作得很好!
//initiate the writer
var sw = new StreamWriter(@"C:\ProgramData\N3RASupportNotifier\Test.csv");
var writer = new CsvWriter(sw);
TableQuery<IssueEntity> query = new TableQuery<IssueEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Issues"));
//Write each record to CSV
foreach (IssueEntity entity in table.ExecuteQuery(query))
{
writer.WriteField(entity.FirstName);
writer.WriteField(entity.LastName);
writer.WriteField(entity.Location);
writer.WriteField(entity.Manager);
writer.WriteField(entity.PartitionKey);
writer.WriteField(entity.PhoneSystem);
writer.WriteField(entity.PrimaryIssue);
writer.WriteField(entity.Timestamp);
writer.NextRecord();
}
为了简单起见,我建议您使用名为 ServiceStack.Text 的第 3 方库来实现您的目的。从 Azure Table 筛选数据后,您可以尝试添加以下代码:
string csvString = CsvSerializer.SerializeToCsv<IssueEntity>(managerQuery);
System.IO.File.WriteAllText(@"C:\temp\csv.csv", csvString);