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?
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 .
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.
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.
Prerequisite
using System.Linq;
then ...
string res = string.Join(Environment.NewLine, results.Rows.OfType<DataRow>().Select(x => string.Join(" ; ", x.ItemArray)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With