How can I convert a single datarow (of a datatable) into a CSV like string with only few C# commands.
In other words into a string like “value_1;value_2;…;value_n”
How can I convert a single datarow (of a datatable) into a CSV like
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Quick and dirty for a single
DataRow:If you do not care about the culture specific separator you may do this to convert a full
DataTable:Here a more complex example if you need to handle a little bit of formatting for the values (in case they’re not just numbers).
Or, if you prefer LINQ (and assuming table is not empty):
Using this private function to format a value. It’s a very naive implementation, for non primitive types you should use
TypeConverter(if any, see this nice library: Universal Type Converter) and quote the text only if needed (2.6):Notes
Even if there is a RFC for CSV many applications does not follow its rules and they handle special cases in their very own way (Microsoft Excel, for example, uses the locale list separator instead of comma and it doesn’t handle newlines in strings as required by the standard).