Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataSet -> Comma/Tab delimited

DataSet -> Comma/Tab delimited

Scheduled Pinned Locked Moved C#
csharp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dratti
    wrote on last edited by
    #1

    I'm writing a small application that needs to take a DataSet and dump it to either a Comma Delimited text file or a Tab delimited text file. I was wondering if there are any "proper" ways to do this, using built in functions or classes, or if I should just manually write out the proper format to a standard text file. Writing it out myself is trivial, but if theres an established way of doing this with .NET I would like to know. Thanks Dave Ratti

    H M 2 Replies Last reply
    0
    • D dratti

      I'm writing a small application that needs to take a DataSet and dump it to either a Comma Delimited text file or a Tab delimited text file. I was wondering if there are any "proper" ways to do this, using built in functions or classes, or if I should just manually write out the proper format to a standard text file. Writing it out myself is trivial, but if theres an established way of doing this with .NET I would like to know. Thanks Dave Ratti

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Nope, just a "proper" way to write it out to XML.

      Reminiscent of my younger years...

      10 LOAD "SCISSORS" 20 RUN

      D 1 Reply Last reply
      0
      • H Heath Stewart

        Nope, just a "proper" way to write it out to XML.

        Reminiscent of my younger years...

        10 LOAD "SCISSORS" 20 RUN

        D Offline
        D Offline
        dratti
        wrote on last edited by
        #3

        Thanks!

        L 1 Reply Last reply
        0
        • D dratti

          Thanks!

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Search for "toub" on GotDotNet.com. He has written a CSV Data"Thingy". Too tired to remember now :zzz: leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          1 Reply Last reply
          0
          • D dratti

            I'm writing a small application that needs to take a DataSet and dump it to either a Comma Delimited text file or a Tab delimited text file. I was wondering if there are any "proper" ways to do this, using built in functions or classes, or if I should just manually write out the proper format to a standard text file. Writing it out myself is trivial, but if theres an established way of doing this with .NET I would like to know. Thanks Dave Ratti

            M Offline
            M Offline
            MrEyes
            wrote on last edited by
            #5

            By now you have probably already solved this problem, but just incase you havent I recently put the following code together to read a file as a dataset and vice versa :

            //read CSV format files into a dataset
            public static DataSet ReadFileAsDataSet(string filePath, string fileName)
            {
            OleDbConnection oCon = new OleDbConnection(
            "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source=" + filePath + ";" +
            "Extended Properties=\"text;HDR=NO;FMT=Delimited\"");

            OleDbDataAdapter oCmd = new OleDbDataAdapter(
              "select \* from "+fileName, oCon);
            

            DataSet ds = new DataSet();
            oCmd.Fill(ds);
            oCon.Close();

            return ds;
            }

            //write a dataset as a file (currently only supports one table)
            //produces files with the following format - "val","val","val","val"
            public static void WriteDataSetAsFile(string filepath, string fileName, DataSet ds)
            {
            if(File.Exists(filepath + "\\" + fileName))
            File.Delete(filepath + "\\" + fileName);

            StreamWriter sw = new StreamWriter(filepath + "\\" + fileName, true);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
            object[] objRowData = dr.ItemArray;
            string strRowData = string.Empty;
            foreach(object obj in objRowData)
            {
            strRowData+= "\"" + obj.ToString() + "\",";
            }
            strRowData = strRowData.Remove(strRowData.Length - 1, 1);
            sw.WriteLine(strRowData);
            }
            sw.Close();
            }

            A 1 Reply Last reply
            0
            • M MrEyes

              By now you have probably already solved this problem, but just incase you havent I recently put the following code together to read a file as a dataset and vice versa :

              //read CSV format files into a dataset
              public static DataSet ReadFileAsDataSet(string filePath, string fileName)
              {
              OleDbConnection oCon = new OleDbConnection(
              "Provider=Microsoft.Jet.OLEDB.4.0;" +
              "Data Source=" + filePath + ";" +
              "Extended Properties=\"text;HDR=NO;FMT=Delimited\"");

              OleDbDataAdapter oCmd = new OleDbDataAdapter(
                "select \* from "+fileName, oCon);
              

              DataSet ds = new DataSet();
              oCmd.Fill(ds);
              oCon.Close();

              return ds;
              }

              //write a dataset as a file (currently only supports one table)
              //produces files with the following format - "val","val","val","val"
              public static void WriteDataSetAsFile(string filepath, string fileName, DataSet ds)
              {
              if(File.Exists(filepath + "\\" + fileName))
              File.Delete(filepath + "\\" + fileName);

              StreamWriter sw = new StreamWriter(filepath + "\\" + fileName, true);

              foreach (DataRow dr in ds.Tables[0].Rows)
              {
              object[] objRowData = dr.ItemArray;
              string strRowData = string.Empty;
              foreach(object obj in objRowData)
              {
              strRowData+= "\"" + obj.ToString() + "\",";
              }
              strRowData = strRowData.Remove(strRowData.Length - 1, 1);
              sw.WriteLine(strRowData);
              }
              sw.Close();
              }

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Thanks!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups