narasiman rao

narasiman rao

  • NA
  • 519
  • 746.7k

how to save database record in to excel using C#

Aug 15 2016 10:27 AM
My code as follows

string str = "Server=(local);initial catalog=testingdb;Trusted_Connection=True";
SqlConnection objConn = new SqlConnection(str);
objConn.Open();
SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From Empdetails", objConn);
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs, SchemaType.Source, "Empdetails");
daAuthors.Fill(dsPubs,"Empdetails");

DataTable tblAuthors;
tblAuthors = dsPubs.Tables"Empdetails"];

foreach (DataRow drCurrent in tblAuthors.Rows)
{
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6}",
drCurrent["ID"].ToString(),
drCurrent["Name"].ToString(),
drCurrent["Address"].ToString(),
drCurrent["City"].ToString(),
drCurrent["Mobile"].ToString(),
drCurrent["Actvie"].ToString(),
drCurrent["Type"].ToString());
}

When i execute the above code in command prompt output as follows

ID Name Address City Mobile Actvie Type
1 Ramya Road Kochi 9840575350 A 1
2 Giriesh Nagar Kochi 9745554689 A 1
3 Vimal BoagRoad Kochi 9645777566 A 1


But i want the above database record to be save in excel,

how to save the above database record into excel.

Answers (5)