Convert Dataset to Textfile tab delimited file - c#

I have a DataSet. I would like to convert dataset column as header and row data as data into a tab delimited text file.
Is there any technique I can do in my end or I have to do the looping manually?
Sincerely Thanks,
- Sel

private static string GetTextFromDataTable(DataTable dataTable)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(string.Join("\t", dataTable.Columns.Cast<DataColumn>().Select(arg => arg.ColumnName)));
foreach (DataRow dataRow in dataTable.Rows)
stringBuilder.AppendLine(string.Join("\t", dataRow.ItemArray.Select(arg => arg.ToString())));
return stringBuilder.ToString();
}
Usage:
var text = GetTextFromDataTable(dataSet.Tables[0]);
File.WriteAllText(filePath, text);

Exporting to XML is built right in, but exporting to CSV, you can use the following code - from http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d2071fd4-8c7d-4d0e-94c3-9586df754df8/
this only writes the data, not the columns, you'll need to loop the column headers first..
Edit: Updated to include column names... I have not run this, and this is an edit from the link above, so it may or may not work, but the concept is here
StringBuilder str = new StringBuilder();
// get the column headers
foreach (var c in NorthwindDataSet.Customers.Columns) {
str.Append("\"" + c.ColumnName.ToString() + "\"\t");
}
str.Append("\r\n");
// write the data here
foreach (DataRow dr in this.NorthwindDataSet.Customers) {
foreach (var field in dr.ItemArray) {
str.Append("\"" + field.ToString() + "\"\t");
}
str.Append("\r\n");
}
try {
My.Computer.FileSystem.WriteAllText("C:\\temp\\testcsv.csv", str.ToString(), false);
} catch (Exception ex) {
MessageBox.Show("Write Error");
}

Note you will need to be using Linq for this solution to work. Add the following using statement to your code:
using System.Linq;

Related

Convert csv to double list with c#

with the following code Im trying to read a csv file that contains double values and convert it into a list. If I want to print that list The output just contains "system.collections.generic.list1 system.string". What is wrong in my code?
var filePath = #"C:\Users\amuenal\Desktop\Uni\test.csv";
var contents = File.ReadAllText(filePath).Split(';');
var csv = from line in contents
select line.Split(';').ToList();
foreach (var i in csv)
{
Console.WriteLine(i);
}
You got a couple things wrong with your code. First, you should most likely be using ReadAllLines() instead of ReadAllText(). Secondly, your LINQ query was returning a List<List<string>> which I imagine is not what you wanted. I would try something like this:
var filePath = #"C:\Users\amuenal\Desktop\Uni\test.csv";
//iterate through all the rows
foreach (var row in File.ReadAllLines(filePath))
{
//iterate through each column in each row
foreach(var col in row.Split(';'))
{
Console.WriteLine(col);
}
}
This should do good. Hope this helps.
var filePath = #"C:\Users\amuenal\Desktop\Uni\test.csv";
var contents = File.ReadAllLines(filePath);
var csv = (from line in contents
select line.Split(';')).SelectMany(x1 => x1);
foreach (var i in csv)
{
Console.WriteLine(i);
}
csv is an IEnumerable of a List of string. (in other words, each "i" is a list of string).
You need two loops:
foreach (var list in csv)
{
foreach(var str in list)
{
Console.WriteLine(str);
}
}

Insert Contents of CSV into strings

I've been struggling with a small piece of code for a little while now. I have a CSV file with one column that contains a string of numbers. I can import that file without issues and display it.
My goal is to take the numbers in each of the tables and put it into a separate string, run that string through a function and then put the results back into my datagrid in column two. Is there a way that I should be doing this using the code below; the foreach statement is where I believe this should be done.
Edit: I tweaked the code and it now works the way that I want it to but I can't insert my result into any columns except for the first one. Is there a way that I should be targeting the results so they go in the second column?
using (var fs = File.OpenRead(Dialog.FileName))
using (var reader = new StreamReader(fs))
{
List<string> lista = new List<string>();
List<string> listb = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
lista.Add(values[0]);
dt1.Rows.Add(values[0]);
}
foreach (var item in lista)
{
string temp;
GetLuhnCheckDigit(item);
listb.Add(last.ToString());
temp = item + last.ToString();
dt1.Rows.Add(temp); //This only adds to the first column
}
dataGridView1.DataSource = dt1;
Without knowing what GetLuhnCheckDigit method does, it is not possible to determine what values you want the second column to contain. Looking at the posted code, there are many things missing like how many columns the data table has, where is the Dialog variable definition? What is last?
Assuming there are at least two columns in the DataTable dt1, I am not sure why you are adding the items to the first column then loop through a list of those items to set the second column. It appears adding both of the columns at the same time would be easier.
You could do all this while reading the file like below:
try {
using (var fs = File.OpenRead(Dialog.FileName)) {
using (var reader = new StreamReader(fs)) {
List<string> lista = new List<string>();
List<string> listb = new List<string>();
string temp;
while (!reader.EndOfStream) {
var line = reader.ReadLine();
var values = line.Split(',');
lista.Add(values[0]);
GetLuhnCheckDigit(values[0]); // <-- What is this method doing???
listb.Add(last.ToString());
temp = values[0] + last.ToString();
dt1.Rows.Add(values[0], temp); // <-- this adds both columns
}
dataGridView1.DataSource = dt1;
}
}
}
catch (Exception e) {
MessageBox.Show("Error: " + e.Message);
}
Let me know if I am missing something, as I am clueless as to what the GetLuhnCheckDigit method could be doing.

Shortest way to save DataTable to Textfile

I just found a few answers for this, but found them all horribly long with lots of iterations, so I came up with my own solution:
Convert table to string:
string myTableAsString =
String.Join(Environment.NewLine, myDataTable.Rows.Cast<DataRow>().
Select(r => r.ItemArray).ToArray().
Select(x => String.Join("\t", x.Cast<string>())));
Then simply save string to text file, for example:
StreamWriter myFile = new StreamWriter("fileName.txt");
myFile.WriteLine(myFile);
myFile.Close();
Is there a shorter / better way?
You have your DataTable named as myDataTable, you can add it to DataSet as:
var dataSet = new DataSet();
dataSet.AddTable(myDataTable);
// Write dataset to xml file or stream
dataSet.WriteXml("filename.xml");
And you can also read from xml file or stream:
dataSet.ReadXml("filename.xml");
#Leonardo sorry but i can 't comment so i post.
Sometimes you can ask the dataset and then work with it. Like this:
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (object item in row.ItemArray)
{
myStreamWriter.Write((string)item + "\t");
}
myStreamWriter.WriteLine();
}
That 's another way but i don 't know which 'll give you a better metric.
If you consider XML as text you can do: myDatatable.WriteXml("mydata.xml") and myDatatable.ReadXml("mydata.xml")
You get an error unless you save it with the schema:
myDataTable.WriteXml("myXmlPath.xml", XmlWriteMode.WriteSchema);
myDatatable.ReadXml("myXmlPath.xml");
There is more info on saving/loading with schema here:
DataTable does not support schema inference from Xml.?

Export several DataTable objects to different .csv files

I would like to export different DataTable objects into different .csv files using a foreach loop and the method "WriteDataTable". The size of the Datatable objects is very small (just 15 columns and 2 rows).
This is the foreach loop:
foreach (var dt in DataTableList)
{
// Name of the file
string fileName = DateTime.Now.Year.ToString();
// Call method
WriteDataTable(fileName, dt);
}
This is the method:
public void WriteDataTable(string fileName, DataTable dt)
{
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>()
.Select(column => column.ColumnName);
sb.AppendLine(string.Join(";", columnNames));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(";", fields));
}
File.WriteAllText(fileName, sb.ToString());
}
The program runs properly but only some of the DataTable objects are really exported. It looks like the previous DataTable object is still being exported when the new DataTable object comes. If I pause the process using "Thread.Sleep (500)" all objects are exported:
foreach (var dt in DataTableList)
{
// Name of the file
string fileName = DateTime.Now.Year.ToString();
// Call method
WriteDataTable(fileName, dt);
// Pause the process
Thread.Sleep (500);
}
Any suggestion to export all the DataTable objects without slowing down the process?.
Thank you in advance.
I found the problem. Since I use "DateTime.Now" (until seconds) as file names, when I export two files within the same second the first export file is overwrited by the second one.

How do I write CSV file with header using FileHelpers?

I am using FileHelpers to write DataTable content into CSV file.
Because of huge number of records in DataTable I chose to dump the result set as it is in DataTable into CSV file like below
CommonEngine.DataTableToCSV(dt, filename)
And the CSV has sucessfully written with 2 million records having the size of 150MB.
But I wanted to add the filed header at first line of this CSV file.
Is there a way FileHelper will allow to write the header using CommonEngine.DataTableToCSV?
You must use engine.HeaderText = engine.GetFileHeader() before you call WriteFile
Looking at the source code, it looks like there is no way of setting the header line. However, it is easy to copy the code and write your own replacement. Something like this:
public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options, string headerLine)
{
using (StreamWriter writer = new StreamWriter(filename, false, options.Encoding, 102400))
{
// output header
writer.Write(headerLine);
writer.Write(StringHelper.NewLine);
foreach (DataRow row in dt.Rows)
{
object[] itemArray = row.ItemArray;
for (int i = 0; i < itemArray.Length; i++)
{
if (i > 0)
{
writer.Write(options.Delimiter);
}
writer.Write(options.ValueToString(itemArray[i]));
}
writer.Write(StringHelper.NewLine);
}
writer.Close();
}
}

Categories