0
using (MySqlCommand cmd = new MySqlCommand("SELECT  EmpId, Emp_Name, Salary , Start_date , End_date , CASE WHEN (Employees.End_Date is null)  THEN DATEDIFF(DATE_ADD(Start_Date, INTERVAL 30 DAY), Start_Date) * Salary/30 ELSE DATEDIFF(End_Date, Start_Date) * Salary/30  END AS Total_Salary From Employees;"))
        {
            using (MySqlDataAdapter sda = new MySqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt); 

                    //Build the CSV file data as a Comma separated string.
                    string csv = string.Empty;

                    foreach (DataColumn column in dt.Columns)
                    {
                        //Add the Header row for CSV file.
                        csv += column.ColumnName + ',';
                    }

                    //Add new line.
                    csv += "\r\n";

                    foreach (DataRow row in dt.Rows)
                    {
                        foreach (DataColumn column in dt.Columns)
                        {
                            //Add the Data rows.
                            csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
                        }

                        //Add new line.
                        csv += "\r\n";
                    }

                    //Download the CSV file.
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
                    Response.Charset = "";
                    Response.ContentType = "application/text";
                    Response.Output.Write(csv);
                    Response.Flush();
                    Response.End();
                    return RedirectToAction("GetAllEmpDetails");
                }
            }
        }

}
2
Contributors
3
Replies
17
Views
3 Months
Discussion Span
Last Post by rproffitt
0

Code dumps without comments or questions are usually unanswered. Add details, questions and where you are having issues.

One observation is that I don't know the size of your database but the way that the variable or array csv is handled (or rather not) it looks like your array could grow without end and be a memory hog. You didn't reveal much at all in your post about the problem so leaving this for others to guess results in bad guesses.

Before you reply, read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question

0

Looked at the image and yes, that looks like what a spreadsheet could show from a CSV file so you did get CSV out but left me to guess what the problems are. The row with the titles is off but that's cosmetic and should be well inside your problem solving skills.

As above, don't make others guess or they will guess badly.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.