In this article we will see how to make bulk
operation in sql server.
First we will see the code to read the database table and take data into the .csv
file
I have just explain this code with one table in my database named csvtable I
have also uploaded simple demo example for that and one csv file to know how the
table is in database.
If you have to perform the same operation on more than one table then simply
make the procedure for that it will make the things easier to perform task on
multiple table.
Here is the code:
protected
void Button1_Click(object
sender, EventArgs e)
{
SqlConnection cn =
new SqlConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString);
SqlDataAdapter adp =
new SqlDataAdapter("select
* from CsvTable", cn);
DataTable dt =
new DataTable();
adp.Fill(dt);
System.IO.StreamWriter sw =
new System.IO.StreamWriter(Server.MapPath("myfile.csv"),false);
int iColCount = dt.Columns.Count;
for (int
i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow
dr in dt.Rows)
{
for (int
i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Response.Redirect("myfile.csv");
}
Now how to read the data from that csv file.
protected void
Button2_Click(object sender,
EventArgs e)
{
string strLine;
string[] strArray;
char[] charArray =
new char[] {
',' };
DataSet ds =
new DataSet();
DataTable dt = ds.Tables.Add("TheData");
string path = Server.MapPath("myfile.csv");
FileStream aFile =
new FileStream(path,
FileMode.Open);
StreamReader sr =
new StreamReader(aFile);
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);
for (int x = 0;
x <= strArray.GetUpperBound(0); x++)
{
dt.Columns.Add(strArray[x].Trim());
}
strLine =
sr.ReadLine();
while (strLine !=
null)
{
strArray = strLine.Split(charArray);
DataRow dr = dt.NewRow();
for (int
i = 0; i <= strArray.GetUpperBound(0); i++)
{
dr[i] = strArray[i].Trim();
}
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
Now how to updating or inserting data from csv file to database.
protected
void Button4_Click(object
sender, EventArgs e)
{
string path = Server.MapPath("myfile.csv");
FileStream aFile =
new FileStream(path,
FileMode.Open);
StreamReader isr = new
StreamReader(aFile);
try
{
SqlConnection cn =
new SqlConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString);
SqlDataAdapter adp =
new SqlDataAdapter("select
* from CsvTable", cn);
DataTable dt =
new DataTable();
adp.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
int totaldatabaserows =
dt.Rows.Count;
string strLine;
string[] strArray;
char[] charArray =
new char[] {
',' };
DataSet ds =
new DataSet();
// DataTable dt = new DataTable();

Abhilash AshokPosted Feb 6, 2011, 11:51 PM
The above mentioned method has its performance drop as well. I would suggest you to take a look at http://blogs.cametoofar.com/post/Bulk-data-InsertUpdateDelete-in-Sql-Server.aspx. Thanks.