Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a DataTable to a string in C#?

Tags:

c#

datatable

I'm using Visual Studio 2005 and have a DataTable with two columns and some rows that I want to output to the console. I hoped there would be something like:

DataTable results = MyMethod.GetResults(); Console.WriteLine (results.ToString()); 

What's the best way (i.e. least amount of coding from me) to convert a simple DataTable to a string?

like image 203
Mark Allison Avatar asked Jul 09 '09 14:07

Mark Allison


People also ask

How to convert DataTable value into string in c#?

You can use it with all default settings just create an instance of a class and call StringifyDataTable method, or you can set additional options if needed. Show activity on this post. If you have a single column in datatable than it's simple to change datatable to string .

How do you reverse a data table?

Reverse For data table, you just need to use a generic For each activity then add . AsEnumerable and change the Argument type to DataRow For each row In dt1. AsEnumerable. Reverse Regards.

What is AC Datatable?

In the ADO.NET library, C# DataTable is a central object. It represents the database tables that provide a collection of rows and columns in grid form.


1 Answers

Prerequisite

using System.Linq; 

then ...

string res = string.Join(Environment.NewLine,      results.Rows.OfType<DataRow>().Select(x => string.Join(" ; ", x.ItemArray))); 
like image 166
ericmas001 Avatar answered Sep 20 '22 10:09

ericmas001