int count = 0;
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandText += " where shifttype = @par ";
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";
System.IO.StreamWriter sw_In = new System.IO.StreamWriter(filePath);
while (reader.Read())
{
if (count == 0)
{
for (int i = 0; i < reader.FieldCount; i++)
{
sw_In.AutoFlush = true;
sw_In.Write(reader.GetName(i) + "\t");
}
sw_In.Write("\n");
count = 1;
}
for (int i = 0; i < reader.FieldCount; i++)
{
sw_In.AutoFlush = true;
sw_In.Write(reader[i].ToString() + "\t");
}
sw_In.Write("\n");
}
}
reader.Close();
sqlConnection.Close();
when i run the above code two excel file is downloaded in desktop under C Folder as follows