Azure表查询到csv c#

Azure表查询到csv c#,c#,csv,export,azure-table-storage,C#,Csv,Export,Azure Table Storage,我正在尝试将Azure表存储查询结果写入一个.csv文件,并将其本地存储在计算机上(temp很好)。我可以毫无疑问地进行查询,然后在messageBox中显示结果。我用c#来做这件事。我不想使用外部应用程序,但如果需要,我将调用powershell脚本。最终,我尝试下载csv,因此我查询csv文件,而不是azure tables存储,以获得更多功能。(目前SQL Server不是一个选项,尽管我知道它会让我的生活变得更轻松) CloudStorageAccount-storageAccount=

我正在尝试将Azure表存储查询结果写入一个.csv文件,并将其本地存储在计算机上(temp很好)。我可以毫无疑问地进行查询,然后在messageBox中显示结果。我用c#来做这件事。我不想使用外部应用程序,但如果需要,我将调用powershell脚本。最终,我尝试下载csv,因此我查询csv文件,而不是azure tables存储,以获得更多功能。(目前SQL Server不是一个选项,尽管我知道它会让我的生活变得更轻松)

CloudStorageAccount-storageAccount=
解析(“DefaultEndpointsProtocol=https;AccountName=MyStorageAccountNameHere;AccountKey=MyTableKeyhere
CloudTableClient tableClient=storageAccount.CreateCloudTableClient();
CloudTable=tableClient.GetTableReference(“TelephonySueLog”);
var managerQuery=new TableQuery()。其中(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();
            }
//启动编写器
var sw=新的StreamWriter(@“C:\ProgramData\N3RASupportNotifier\Test.csv”);
var编写器=新的CSV编写器(sw);
TableQuery query=new TableQuery()。其中(TableQuery.GenerateFilterCondition(“PartitionKey”,QueryComparisons.Equal,“Issues”);
//将每条记录写入CSV
foreach(表中的IssueEntity实体。ExecuteQuery(查询))
{
writer.WriteField(entity.FirstName);
writer.WriteField(entity.LastName);
writer.WriteField(entity.Location);
writer.WriteField(entity.Manager);
writer.WriteField(entity.PartitionKey);
WriteField(entity.PhoneSystem);
writer.WriteField(entity.primarysissue);
WriteField(entity.Timestamp);
writer.NextRecord();
}
我真的弄明白了: 从顶部开始

    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();
            }
//启动编写器
var sw=新的StreamWriter(@“C:\ProgramData\N3RASupportNotifier\Test.csv”);
var编写器=新的CSV编写器(sw);
TableQuery query=new TableQuery()。其中(TableQuery.GenerateFilterCondition(“PartitionKey”,QueryComparisons.Equal,“Issues”);
//将每条记录写入CSV
foreach(表中的IssueEntity实体。ExecuteQuery(查询))
{
writer.WriteField(entity.FirstName);
writer.WriteField(entity.LastName);
writer.WriteField(entity.Location);
writer.WriteField(entity.Manager);
writer.WriteField(entity.PartitionKey);
WriteField(entity.PhoneSystem);
writer.WriteField(entity.primarysissue);
WriteField(entity.Timestamp);
writer.NextRecord();
}

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

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

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

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