9

I had a look on the site and on Google, but I couldn't seem to find a good solution to what I'm trying to do.

Basically, I have a client server application (C#) where I send the server an SQL select statement (Connecting to SQL Server 2008) and would like to return results in a CSV manner back to the client.

So far I have the following:

if (sqlDataReader.HasRows)
{
    while(sqlDataReader.Read())
    {
       //not really sure what to put here and if the while should be there!
    }

} `

Unfortunately, I'm really new to connecting C# with SQL. I need any tips on how to simply put the results in a string in a csv format. The columns and fields are likely to be different so I cannot use the method of something[something] as I've seen in a few sites. I'm not sure if I'm being comprehensible tbh!

I would really appreciate any tips / points on how to go about this please!

1
  • if PowerShell is an option for you it has both SQL Server modules and a built-in CSV writer. Some POSH genuious can probably tell you how to do it with one statement. Commented Dec 26, 2009 at 17:14

5 Answers 5

17

Here is a method I use to dump any IDataReader out to a StreamWriter. I generally create the StreamSwriter like this: new StreamWriter(Response.OutputStream). I convert any double-quote characters in the input into single-quote characters (maybe not the best way to handle this, but it works for me).

public static void createCsvFile(IDataReader reader, StreamWriter writer) {
    string Delimiter = "\"";
    string Separator = ",";

    // write header row
    for (int columnCounter = 0; columnCounter < reader.FieldCount; columnCounter++) {
        if (columnCounter > 0) {
            writer.Write(Separator);
        }
        writer.Write(Delimiter + reader.GetName(columnCounter) + Delimiter);
    }
    writer.WriteLine(string.Empty);

    // data loop
    while (reader.Read()) {
        // column loop
        for (int columnCounter = 0; columnCounter < reader.FieldCount; columnCounter++) {
            if (columnCounter > 0) {
                writer.Write(Separator);
            }
            writer.Write(Delimiter + reader.GetValue(columnCounter).ToString().Replace('"', '\'') + Delimiter);
        }   // end of column loop
        writer.WriteLine(string.Empty);
    }   // data loop

    writer.Flush();
}
4
  • Thanks! I adapted this to my solution and it works like a charm! thanks so much
    – Erika
    Commented Dec 27, 2009 at 10:53
  • This answer is wrong! It does not ENCODE the content into CSV and doesn't even handle commas in the underlying dataset...
    – A X
    Commented Aug 5, 2020 at 4:33
  • I deal with commas by wrapping all data items in quotes. I am not sure why I would need to encode the data - maybe some apps would require that, but mine does not.
    – Ray
    Commented Aug 6, 2020 at 12:04
  • Josh Close has a very similar answer using CsvWriter class from his CsvHelper package which I found worked well. stackoverflow.com/questions/14882796/datatable-to-csv#_=_ Commented Feb 14, 2023 at 2:07
2

As mentioned, there are quite a few issues with delimiters, escaping characters correctly, and formatting different types correctly. But if you are just looking for an example of putting data into a string, here is yet another one. It does not do any checking for the aforementioned complications.

  public static void ReaderToString( IDataReader Reader )
     {
     while ( Reader.Read() )
        {
        StringBuilder str = new StringBuilder();
        for ( int i = 0; i < Reader.FieldCount; i++ )
           {

           if ( Reader.IsDBNull( i ) )
              str.Append( "null" );
           else
              str.Append( Reader.GetValue( i ).ToString() );

           if ( i < Reader.FieldCount - 1 )
              str.Append( ", " );

           }
        // do something with the string here
        Console.WriteLine(str);
        }
     }
2

When dealing with CSV file I usually go for the FileHelpers library: it has a SqlServerStorage class which you can use to read records from a SQL server and write them to a CSV file.

1

You may be able to adapt the implementation of a CSV writer available here.

If you also need to parse CSV files, the implementation here is relatively good.

The CSV format is more complicated than it looks - particularly if you're going to deal with arbitrary data coming back from a query. You would need to be able to handle escaping of special characters (like quotes and commas), dealing with line breaks, and the like. You are better off finding and using a proven implementation - especially if you're new to C#.

1
  • 1
    Link is broken. Kindly suggest new one if any there.
    – Ashok
    Commented Nov 16, 2017 at 3:32
1

You can get the table column names like this:

SqlConnection conn = new SqlConnection(connString);
conn.Open();

SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();

DataTable schema = rdr.GetSchemaTable();
foreach (DataRow row in schema.Rows) 
{
    foreach (DataColumn col in schema.Columns)
        Console.WriteLine(col.ColumnName + " = " + row[col]);
}

rdr.Close()
conn.Close();

Of course you can determine the columns names with the first row only, here it does it on every rows.

You can now put your own code to join the columns into a CSV line pretty easily...

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.