2016-09-02 2 views
0

Ich versuche, eine Azure-Tabelle Storage Query-Ergebnis in eine CSV-Datei zu schreiben und lokal auf der Maschine zu speichern (Temp ist in Ordnung). Ich bin in der Lage, ohne Problem abzufragen - dann zeigen Sie die Ergebnisse in einer messageBox an. Ich mache das mit C#. Ich möchte keine externe Anwendung verwenden, werde aber ein Powershell-Skript aufrufen, wenn dies erforderlich ist. Letztendlich versuche ich den CSV herunterzuladen, also frage ich die CSV-Datei statt azurblauen Tabellenspeicher für mehr Funktionalität ab. (SQL Server ist keine Option zu diesem Zeitpunkt - obwohl ich es verstehen würde mein Leben so viel einfacher machen)Azure-Tabellenabfrage nach csv C#

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

Antwort

1

ich es tatsächlich herausgefunden: beginnend mit dem oben

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

Dann kommt die Abfrage und CSV Edit:

 //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"); 

Arbeitete wie ein Charme!

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

Für eine einfache Art und Weise, empfehle ich Ihnen eine 3rd-Party-Bibliothek ServiceStack.Text Ihr Ziel zu erreichen genannt werden. Nachdem Sie die Daten aus Azure Table gefiltert haben, können Sie versuchen, den folgenden Code hinzuzufügen:

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