Hi seriverma,
Refer the below modified sample.
VB.Net
Protected Sub ExportCSV(sender As Object, e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT TOP 1 'aaa' column1,'563210,jjj,lll' column2 FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
'Build the CSV file data as a Comma separated string.
Dim csv As String = String.Empty
For Each column As DataColumn In dt.Columns
'Add the Header row for CSV file.
csv += column.ColumnName + ","c
Next
'Add new line.
csv += vbCr & vbLf
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
'Add the Data rows.
csv += AddEscapeSequenceInCsvField(row(column.ColumnName).ToString()) + ","
Next
'Add new line.
csv += vbCr & vbLf
Next
'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()
End Using
End Using
End Using
End Using
End Sub
Private Function AddEscapeSequenceInCsvField(ValueToEscape As String) As String
If ValueToEscape.Contains(",") Then
'Return (Convert.ToString("""") & ValueToEscape) + """"
Return String.Format("{0}{1}{2}", """""""", ValueToEscape, """""""")
ElseIf ValueToEscape.Contains("+") Then
Return (Convert.ToString("""") & ValueToEscape) + """"
Else
Return ValueToEscape
End If
End Function
C#
protected void ExportCSV(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 'aaa' column1,'563210,jjj,lll' column2 FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
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 += AddEscapeSequenceInCsvField(row[column.ColumnName].ToString()) + ",";
}
//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();
}
}
}
}
}
private string AddEscapeSequenceInCsvField(string ValueToEscape)
{
if (ValueToEscape.Contains(","))
{
//return (Convert.ToString("\"") + ValueToEscape) + "\"";
return string.Format("{0}{1}{2}", "\"\"\"", ValueToEscape, "\"\"\"");
}
else if (ValueToEscape.Contains("+"))
{
return (Convert.ToString("\"") + ValueToEscape) + "\"";
}
else
{
return ValueToEscape;
}
}
Screenshot