5 Replies - 2229 Views - Last Post: 09 June 2012 - 09:56 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Read a delimited file into a DataSet

Posted 28 February 2008 - 05:22 PM

Description: For this snippet you need to add a reference to the System.IO Namespace. Then you pass it the full path to the file, the name you want your DataTable and the delimiter to split the file on, it will then split the first row (for the columns) and before adding the columns it will make sure the column doesn't already exist. From there it will populate your DataSet and return it to you.This is a method for reading a the contents of a text file into a DataSet

//Namespace reference
using System.IO;
using System.Data;

/// <summary>
/// method to read a text file into a DataSet
/// </summary>
/// <param name="file">file to read from</param>
/// <param name="tableName">name of the DataTable we want to add</param>
/// <param name="delimeter">delimiter to split on</param>
/// <returns>a populated DataSet</returns>
public DataSet BuildDataSet(string file,string tableName,string delimeter)
{
    //create our DataSet
    DataSet domains = new DataSet();
    //add our table
    domains.Tables.Add(tableName);
    try
    {
        //first make sure the file exists
        if (File.Exists(file))
        {
            //create a StreamReader and open our text file
            StreamReader reader = new StreamReader(file);
            //read the first line in and split it into columns
            string[] columns = reader.ReadLine().Split(delimeter.ToCharArray());
            //now add our columns (we will check to make sure the column doesnt exist before adding it)
            foreach (string col in columns)
            {
                //variable to determine if a column has been added
                bool added = false;
                string next = "";
                //our counter
                int i = 0;
                while (!(added))
                {
                    string columnName = col;
                    //now check to see if the column already exists in our DataTable
                    if (!(domains.Tables[tableName].Columns.Contains(columnName)))
                    {
                        //since its not in our DataSet we will add it
                        domains.Tables[tableName].Columns.Add(columnName, typeof(string));
                    }
                    else
                    {
                        //we didnt add the column so increment out counter
                        i++;
                    }
                }
            }
            //now we need to read the rest of the text file
            string data = reader.ReadToEnd();
            //now we will split the file on the carriage return/line feed
            //and toss it into a string array
            string[] rows = data.Split("rn".ToCharArray());
            //now we will add the rows to our DataTable
            foreach (string r in rows)
            {
                string[] items = r.Split(delimeter.ToCharArray());
                //split the row at the delimiter
                domains.Tables[tableName].Rows.Add(items);
            }
        }
        else
        {
            throw new FileNotFoundException("The file " + file + " could not be found");
        }
        
    }
    catch (FileNotFoundException ex)
    {
        _message = ex.Message;
        return null;
    }
    catch (Exception ex)
    {
        _message = ex.Message;
        return null;
    }
    
    //now return the DataSet
    return domains;
}

//Sample usage

//for a Windows application
DataSet data = BuildDataSet("C:MyFile.txt","MyTable",",");

//For an ASP.Net application
DataSet data = BuildDataSet(Server.MapPath("MyFile.txt"),"MyTable",",");


Is This A Good Question/Topic? 0
  • +

Replies To: Read a delimited file into a DataSet

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Read a delimited file into a DataSet

Posted 28 February 2008 - 05:22 PM

Description: For this snippet you need to add a reference to the System.IO Namespace. Then you pass it the full path to the file, the name you want your DataTable and the delimiter to split the file on, it will then split the first row (for the columns) and before adding the columns it will make sure the column doesn't already exist. From there it will populate your DataSet and return it to you.This is a method for reading a the contents of a text file into a DataSet

//Namespace reference
using System.IO;
using System.Data;

#region BuildDataSet
/// <summary>
/// method to read a text file into a DataSet
/// </summary>
/// <param name="file">file to read from</param>
/// <param name="tableName">name of the DataTable we want to add</param>
/// <param name="delimeter">delimiter to split on</param>
/// <returns>a populated DataSet</returns>
public DataSet BuildDataSet(string file,string tableName,string delimeter)
{
    //create our DataSet
    DataSet domains = new DataSet();
    //add our table
    domains.Tables.Add(tableName);
    try
    {
        //first make sure the file exists
        if (File.Exists(file))
        {
            //create a StreamReader and open our text file
            StreamReader reader = new StreamReader(file);
            //read the first line in and split it into columns
            string[] columns = reader.ReadLine().Split(delimeter.ToCharArray());
            //now add our columns (we will check to make sure the column doesnt exist before adding it)
            foreach (string col in columns)
            {
                //variable to determine if a column has been added
                bool added = false;
                string next = "";
                //our counter
                int i = 0;
                while (!(added))
                {
                    string columnName = col;
                    //now check to see if the column already exists in our DataTable
                    if (!(domains.Tables[tableName].Columns.Contains(columnName)))
                    {
                        //since its not in our DataSet we will add it
                        domains.Tables[tableName].Columns.Add(columnName, typeof(string));
                        added = true;
                    }
                    else
                    {
                        //we didnt add the column so increment out counter
                        i++;
                    }
                }
            }
            //now we need to read the rest of the text file
            string data = reader.ReadToEnd();
            //now we will split the file on the carriage return/line feed
            //and toss it into a string array
            string[] rows = data.Split("r".ToCharArray());
            //now we will add the rows to our DataTable
            foreach (string r in rows)
            {
                string[] items = r.Split(delimeter.ToCharArray());
                //split the row at the delimiter
                domains.Tables[tableName].Rows.Add(items);
            }
        }
        else
        {
            throw new FileNotFoundException("The file " + file + " could not be found");
        }
        
    }
    catch (FileNotFoundException ex)
    {
        _message = ex.Message;
        return null;
    }
    catch (Exception ex)
    {
        _message = ex.Message;
        return null;
    }
    
    //now return the DataSet
    return domains;
}
#endregion


//Sample usage

//for a Windows application
DataSet data = BuildDataSet("C:MyFile.txt","MyTable",",");

//For an ASP.Net application
DataSet data = BuildDataSet(Server.MapPath("MyFile.txt"),"MyTable",",");

Was This Post Helpful? 0
  • +
  • -

#3 ibrahem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 04-May 08

Re: Read a delimited file into a DataSet

Posted 04 May 2008 - 06:54 PM

Hi, Good code, but I have a question I have text file that I need to convert to array The file contains data [text], columns [doubles], and I don't know how much text (lines)? or how many columns? I have to say that your code was great . Thanks
Was This Post Helpful? 0
  • +
  • -

#4 praneeshpeeyar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-July 08

Re: Read a delimited file into a DataSet

Posted 08 February 2010 - 10:55 PM

Please specify more about the secondparameter, table name. The type of it is string but in the form code view the data table can't converted to string. I am waiting here for your reply..!
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Read a delimited file into a DataSet

Posted 09 February 2010 - 06:43 AM

If you loook at the comments at the top of the snippet you'll see this: /// name of the DataTable we want to add That should tell you what that parameter is for.
Was This Post Helpful? 0
  • +
  • -

#6 praneeshpeeyar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-July 08

Re: Read a delimited file into a DataSet

Posted 11 February 2010 - 12:19 AM

haii,First of all thank u for your reply. Let me go to my doubt. I added the data table name. But it is not working The out put is a empty grid. Please help me as soon as possible.
Was This Post Helpful? 0
  • +
  • -

#7 drob08  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Re: Read a delimited file into a DataSet

Posted 09 June 2012 - 09:56 AM

_exmessage is giving me an error is that a variable you declared or what is that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1