Azure表查询到CSV C#

yfwxisqw  于 2023-03-27  发布在  C#
关注(0)|答案(3)|浏览(118)

我正在尝试将Azure表存储查询结果写入.csv文件并将其本地存储在计算机上(temp是好的).我能够毫无问题地查询-然后在messageBox中显示结果.我使用c#做到这一点.我不想使用外部应用程序,但如果需要的话会调用powershell脚本.最终,我试图下载csv,所以我查询csv文件而不是Azure表存储以获得更多功能。(SQL Server目前不是一个选择-尽管我知道它会让我的生活变得更容易)

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);
nwnhqdif

nwnhqdif1#

其实我已经想通了:从顶部开始

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();
            }
eqoofvh9

eqoofvh92#

为了简单起见,我建议您使用名为ServiceStack.Text的第三方库来实现您的目的。在从Azure表中筛选数据后,您可以尝试添加以下代码:

string csvString = CsvSerializer.SerializeToCsv<IssueEntity>(managerQuery);
System.IO.File.WriteAllText(@"C:\temp\csv.csv", csvString);
dffbzjpn

dffbzjpn3#

我为Azure.Data.Tables SDK编写了一个小扩展,以简化与Azure Storage Explorer兼容的CSV导出:

using Azure.Data.Tables;
using Medienstudio.Azure.Data.Tables.CSV;

TableServiceClient tableServiceClient = new(connectionString);
TableClient tableClient = tableServiceClient.GetTableClient("tablename");

// Export all rows from the table to a CSV file
using StreamWriter writer = File.CreateText("test.csv");
await _tableClient.ExportCSVAsync(writer);

// Export all rows as CSV to Azure BLob Storage
BlobContainerClient containerClient = new(BlobConnectionString, "testcontainer");
var blobClient = containerClient.GetBlobClient("test.csv");
var stream = await blobClient.OpenWriteAsync(true, new BlobOpenWriteOptions() { HttpHeaders = new BlobHttpHeaders { ContentType = "text/csv" } });
using StreamWriter writer = new(stream);
await _tableClient.ExportCSVAsync(writer);

// Import all rows from a CSV file to the table
using StreamReader reader = new("test.csv");
await _tableClient.ImportCSVAsync(reader);

源代码nuget

相关问题