Fabio Silva

Fabio Silva

  • 2k
  • 19
  • 225

Create individual files from mysql query results

May 7 2020 3:17 PM
hello, can anyone help me with this one?
 
I have made a mysql connection and results display, however i need to create a txt tile for each result, with the result as file name
 
Example:
mysql query returns "123123123", then file 123123123.txt woud be created.
 
here is my code as it stands
 
  1. string[] filePaths = Directory.GetFiles(@"C:\Users\travi\Desktop\customers\Active");  
  2.             foreach (string filePath in filePaths)  
  3.                 File.Delete(filePath);  
  4.             string serverIp = "localhost";  
  5.             string username = "root";  
  6.             string password = "";  
  7.             string databaseName = "teste";  
  8.   
  9.             string dbConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};", serverIp, username, password, databaseName);  
  10.             string query = "SELECT ID FROM `wp_posts` WHERE `post_type` = 'shop_subscription' and post_status = 'wc-active'";  
  11.   
  12.             var conn = new MySql.Data.MySqlClient.MySqlConnection(dbConnectionString);  
  13.             conn.Open();  
  14.   
  15.             var cmd = new MySql.Data.MySqlClient.MySqlCommand(query, conn);  
  16.             var reader = cmd.ExecuteReader();  
  17.   
  18.             while (reader.Read())  
  19.             {  
  20.                 var subs = reader["ID"];  
  21.   
  22.                 // how to create individual files from each result  
  23.                    
  24.               
 

Answers (1)