using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace testcsv2table
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Aimsweb(string t)
{
string sep = ",";
string tablename = string.Empty;
string fileloc = string.Empty;
string filename = string.Empty;
string connection = string.Empty;
if(t=="abc")
{
tablename = Properties.Settings.Default.Aimsweb_view;
fileloc = Properties.Settings.Default.filelocation;
filename =tablename+ Properties.Settings.Default.Aimsweb_Csvfilename;
// string dateflag = string.Empty;
connection = Properties.Settings.Default.connectionstring;
}
if (t == "iShrissDB")
{
tablename = Properties.Settings.Default.Aimsweb_view1;
fileloc = Properties.Settings.Default.filelocation;
filename = tablename + Properties.Settings.Default.Aimsweb_Csvfilename;
connection = Properties.Settings.Default.connectionstring2;
}
fileloc = fileloc + @"\";
string fl = fileloc + filename + ".csv";//make changes in settings.settings file to change in fileloc, filename (fileloc, filename) column
SqlConnection con = new SqlConnection(connection); //make changes in settings.settings file to change the connection in(connectionstring) column
SqlCommand cmd = new SqlCommand("Select * from " + tablename, con); //make changes in settings.settings file to change for tablename in(tablename) column
con.Open();
try
{
SqlDataReader dr = cmd.ExecuteReader();
int fields = dr.FieldCount - 1;
string data = string.Empty;
string g = string.Empty;
while (dr.Read())
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= fields; i++)
{
if (i == fields)
{
sep = " ";
}
sb.Append(dr[i].ToString() + sep);
data = sb.ToString();
}
g += data;
g = g.Replace(" ",System.Environment.NewLine);
System.IO.File.WriteAllText(@fl, g);
sep = ",";
}
con.Close();
// MessageBox.Show("done");
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Aimsweb("abc");
Aimsweb("iShrissDB");
}
}
}This snippets is for exporting data into CSV.
