TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Export Table Data from Multiple Databases in CSV
Shridhar Sharma
Jul 02
2015
Code
904
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
testcsv2table.rar
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"
);
}
}
}
csv
sql
window application
export