Hold On

Hold On

  • 1.2k
  • 857
  • 107k

Export Postgres Table to csv File using C#

Jul 11 2020 12:33 AM
I tried to export table values from Postgres to csv file using c#. Below is my code;
 
  1. private void ExportFileFromLocalDb1()  
  2.         {  
  3.             // Export path and file.  
  4.             string exportPath = "D:\\JESAP files\\LTS Project\\Test\\"//D:\JESAP files\LTS Project\Test  
  5.             string exportCsv = "transaction.csv";  
  6.   
  7.             // Stream writer for CSV file.  
  8.             StreamWriter csvFile = null;  
  9.   
  10.             // Check to see if the file path exists.  
  11.             if (Directory.Exists(exportPath))  
  12.             {  
  13.   
  14.                 try  
  15.                 {  
  16.                     using (NpgsqlConnection con = ClassConnection.GetDbCon())  
  17.                     {  
  18.                         NpgsqlCommand cmd = new NpgsqlCommand("SELECT id, date, bankbooknumber, transaction_code_id, amount, debit_acc_number, credit_acc_number, user_id, vbcode, synchronized, description FROM public.transactions;", con);  
  19.                         cmd.CommandType = CommandType.Text;  
  20.                         con.Open();  
  21.                         NpgsqlDataReader rdr = cmd.ExecuteReader();  
  22.   
  23.                         // Stream writer for CSV file.  
  24.                         csvFile = new StreamWriter(@exportPath + exportCsv);  
  25.   
  26.                         // Add the headers to the CSV file.  
  27.                         csvFile.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\"",  
  28.                             rdr.GetName(0),  
  29.                             rdr.GetName(1),  
  30.                             rdr.GetName(2),  
  31.                             rdr.GetName(3),  
  32.                             rdr.GetName(4),  
  33.                             rdr.GetName(5),  
  34.                             rdr.GetName(6),  
  35.                             rdr.GetName(7),  
  36.                             rdr.GetName(8),  
  37.                             rdr.GetName(9),  
  38.                             rdr.GetName(10)  
  39.                             ));  
  40.   
  41.                         // Construct CSV file data rows.  
  42.                         while (rdr.Read())  
  43.                         {  
  44.   
  45.                             // Add line from reader object to new CSV file.  
  46.                             csvFile.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\"",  
  47.                             rdr.GetName(0),  
  48.                             rdr.GetName(1),  
  49.                             rdr.GetName(2),  
  50.                             rdr.GetName(3),  
  51.                             rdr.GetName(4),  
  52.                             rdr.GetName(5),  
  53.                             rdr.GetName(6),  
  54.                             rdr.GetName(7),  
  55.                             rdr.GetName(8),  
  56.                             rdr.GetName(9),  
  57.                             rdr.GetName(10)  
  58.                             ));  
  59.   
  60.                         }  
  61.   
  62.                         // Message stating export successful.  
  63.                         MessageBox.Show("Data export successful.");  
  64.                     }  
  65.                 }  
  66.                 catch (Exception e)  
  67.                 {  
  68.   
  69.                     throw e;  
  70.   
  71.                 }  
  72.                 finally  
  73.                 {  
  74.                     csvFile.Close();  
  75.                 }  
  76.   
  77.             }  
  78.             else  
  79.             {  
  80.   
  81.                 // Display a message stating file path does not exist.  
  82.                 MessageBox.Show("File path does not exist.");  
  83.   
  84.             }  
  85.         }  
the export is successful, but, I only get the header for all rows. See image below:
 
Please help. 

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (3)