0

i have text file that looks like this:

1 \t a
2 \t b
3 \t c
4 \t d

i have dataset: DataSet ZX = new DataSet();

is there any way for inserting the text file values to this dataset ?

thanks in advance

Gold
  • 60,526
  • 100
  • 215
  • 315

5 Answers5

2

Sure there is,

Define a DataTable, Add DataColumn with data types that you want,

ReadLine the file, split the values by tab, and add each value as a DataRow to DataTable by calling NewRow.

There is a nice sample code at MSDN, take a look and follow the steps

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
2

You will have to parse the file manually. Maybe like this:

string data = System.IO.File.ReadAllText("myfile.txt");

DataRow row = null;
DataSet ds = new DataSet();
DataTable tab = new DataTable();

tab.Columns.Add("First");
tab.Columns.Add("Second");

string[] rows = data.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string r in rows)
{
    string[] columns = r.Split(new char[] { '\t' },   StringSplitOptions.RemoveEmptyEntries);
    if (columns.Length <= tab.Columns.Count)
    {
        row = tab.NewRow();

        for (int i = 0; i < columns.Length; i++)
            row[i] = columns[i];

         tab.Rows.Add(row);
     }
 }

 ds.Tables.Add(tab);

UPDATE

If you don't know how many columns in the text file you can modify my original example as the following (assuming that the number of columns is constant for all rows):

// ...
string[] columns = r.Split(new char[] { '\t' },  StringSplitOptions.RemoveEmptyEntries);
if (tab.Columns.Count == 0)
{
    for(int i = 0; i < columns.Length; i++)
        tab.Columns.Add("Column" + (i + 1));
}

if (columns.Length <= tab.Columns.Count)
{
// ... 

Also remove the initial creation of table columns:

// tab.Columns.Add("First");
// tab.Columns.Add("Second")

-- Pavel

volpav
  • 5,090
  • 19
  • 27
  • thanks for the help, but what i can do when i dont know how many columns in my text file ? – Gold Jan 07 '11 at 09:10
1

Yes, create data tabel on the fly, refer this article for how-to

Read your file line by line and add those value to your data table , refer this article for how-to read text file

Nikhil Vaghela
  • 920
  • 2
  • 11
  • 36
0

I'd like also to add to the "volpan" code the following :

String _source = System.IO.File.ReadAllText(FilePath, Encoding.GetEncoding(1253));

It's good to add the encoding of your text file, so you can be able to read the data and in my case export those after modification to another file.

0

Try this

private DataTable GetTextToTable(string path)
{
    try
    {
        DataTable dataTable = new DataTable
        {
            Columns = {
                {"MyID", typeof(int)},
                "MyData"
            },
            TableName="MyTable"
        };
        // Create an instance of StreamReader to read from a file.
        // The using statement also closes the StreamReader.
        using (StreamReader sr = new StreamReader(path))
        {
            String line;
            // Read and display lines from the file until the end of
            // the file is reached.
            while ((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split(new string[] { "\\t" }, StringSplitOptions.RemoveEmptyEntries);
                dataTable.Rows.Add(words[0], words[1]);
            }
        }
        return dataTable;
    }
    catch (Exception e)
    {
        // Let the user know what went wrong.
        throw new Exception(e.Message);
    }

}

Call it like

GetTextToTable(Path.Combine(Server.MapPath("."), "TextFile.txt"));

You could also check out CSV File Imports in .NET

Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251