Export Table Data from Multiple Databases in CSV

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. namespace testcsv2table  
  12. {  
  13.    public partial class Form1 : Form  
  14.    {  
  15.       public Form1()  
  16.       {  
  17.          InitializeComponent();  
  18.       }  
  19.       public void Aimsweb(string t)  
  20.       {  
  21.          string sep = ",";  
  22.          string tablename = string.Empty;  
  23.          string fileloc = string.Empty;  
  24.          string filename = string.Empty;  
  25.          string connection = string.Empty;  
  26.          if(t=="abc")  
  27.          {  
  28.             tablename = Properties.Settings.Default.Aimsweb_view;  
  29.             fileloc = Properties.Settings.Default.filelocation;  
  30.             filename =tablename+ Properties.Settings.Default.Aimsweb_Csvfilename;  
  31.             // string dateflag = string.Empty;  
  32.             connection = Properties.Settings.Default.connectionstring;  
  33.          }  
  34.          if (t == "iShrissDB")  
  35.          {  
  36.             tablename = Properties.Settings.Default.Aimsweb_view1;  
  37.             fileloc = Properties.Settings.Default.filelocation;  
  38.             filename = tablename + Properties.Settings.Default.Aimsweb_Csvfilename;  
  39.             connection = Properties.Settings.Default.connectionstring2;  
  40.          }  
  41.          fileloc = fileloc + @"\";  
  42.          string fl = fileloc + filename + ".csv";//make changes in settings.settings file to change in fileloc, filename (fileloc, filename) column  
  43.          SqlConnection con = new SqlConnection(connection); //make changes in settings.settings file to change the connection in(connectionstring) column  
  44.          SqlCommand cmd = new SqlCommand("Select * from " + tablename, con); //make changes in settings.settings file to change for tablename in(tablename) column  
  45.          con.Open();  
  46.          try  
  47.          {  
  48.             SqlDataReader dr = cmd.ExecuteReader();  
  49.             int fields = dr.FieldCount - 1;  
  50.             string data = string.Empty;  
  51.             string g = string.Empty;  
  52.             while (dr.Read())  
  53.             {  
  54.                StringBuilder sb = new StringBuilder();  
  55.                for (int i = 0; i <= fields; i++)  
  56.                {  
  57.                   if (i == fields)  
  58.                   {  
  59.                      sep = " ";  
  60.                   }  
  61.                   sb.Append(dr[i].ToString() + sep);  
  62.                   data = sb.ToString();  
  63.                }  
  64.                g += data;  
  65.                g = g.Replace(" ",System.Environment.NewLine);  
  66.                System.IO.File.WriteAllText(@fl, g);  
  67.                sep = ",";  
  68.             }  
  69.             con.Close();  
  70.             // MessageBox.Show("done");  
  71.          }  
  72.          catch (Exception ec)  
  73.          {  
  74.             MessageBox.Show(ec.Message);  
  75.          }  
  76.       }  
  77.       private void Form1_Load(object sender, EventArgs e)  
  78.       {  
  79.       }  
  80.       private void button1_Click(object sender, EventArgs e)  
  81.       {  
  82.          Aimsweb("abc");  
  83.          Aimsweb("iShrissDB");  
  84.       }  
  85.    }  
  86. }