Click here to Skip to main content
15,892,059 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.7K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion
#undef GENERICS
//#define GENERICS
//#if NET_2_0

#region "  � Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar" 

// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.

#endregion

using System;
using System.Data;
using System.IO;
using Transformalize.Libs.FileHelpers.Helpers;
using Transformalize.Libs.FileHelpers.RunTime;

#if ! MINI

#endif

namespace Transformalize.Libs.FileHelpers.Engines
{
    /// <summary>A class to read generic CSV files delimited for any char.</summary>
#if NET_2_0
    [DebuggerDisplay("CsvEngine. ErrorMode: {ErrorManager.ErrorMode.ToString()}. Encoding: {Encoding.EncodingName}")]
#endif
    public sealed class CsvEngine : FileHelperEngine
    {
        #region "  Static Methods  "

        /// <summary>Reads a Csv File and return their contents as DataTable (The file must have the field names in the first row)</summary>
        /// <param name="delimiter">The delimiter for each field</param>
        /// <param name="filename">The file to read.</param>
        /// <returns>The contents of the file as a DataTable</returns>
        public static DataTable CsvToDataTable(string filename, char delimiter)
        {
            return CsvToDataTable(filename, "RecorMappingClass", delimiter, true);
        }

        /// <summary>Reads a Csv File and return their contents as DataTable (The file must have the field names in the first row)</summary>
        /// <param name="classname">The name of the record class</param>
        /// <param name="delimiter">The delimiter for each field</param>
        /// <param name="filename">The file to read.</param>
        /// <returns>The contents of the file as a DataTable</returns>
        public static DataTable CsvToDataTable(string filename, string classname, char delimiter)
        {
            return CsvToDataTable(filename, classname, delimiter, true);
        }


        /// <summary>Reads a Csv File and return their contents as DataTable</summary>
        /// <param name="classname">The name of the record class</param>
        /// <param name="delimiter">The delimiter for each field</param>
        /// <param name="filename">The file to read.</param>
        /// <param name="hasHeader">Indicates if the file contains a header with the field names.</param>
        /// <returns>The contents of the file as a DataTable</returns>
        public static DataTable CsvToDataTable(string filename, string classname, char delimiter, bool hasHeader)
        {
            var options = new CsvOptions(classname, delimiter, filename);
            if (hasHeader == false) options.HeaderLines = 0;
            return CsvToDataTable(filename, options);
        }

        /// <summary>Reads a Csv File and return their contents as DataTable</summary>
        /// <param name="filename">The file to read.</param>
        /// <param name="options">The options used to create the record mapping class.</param>
        /// <returns>The contents of the file as a DataTable</returns>
        public static DataTable CsvToDataTable(string filename, CsvOptions options)
        {
            var engine = new CsvEngine(options);
            return engine.ReadFileAsDT(filename);
        }


        /// <summary>Simply dumps the DataTable contents to a delimited file using a ',' as delimiter.</summary>
        /// <param name="dt">The source Data Table</param>
        /// <param name="filename">The destination file.</param>
        public static void DataTableToCsv(DataTable dt, string filename)
        {
            DataTableToCsv(dt, filename, new CsvOptions("Tempo1", ',', dt.Columns.Count));
        }


        /// <summary>Simply dumps the DataTable contents to a delimited file. Only allows to set the delimiter.</summary>
        /// <param name="dt">The source Data Table</param>
        /// <param name="filename">The destination file.</param>
        /// <param name="options">The options used to write the file</param>
        public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options)
        {
            using (var fs = new StreamWriter(filename, false, options.Encoding))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    var fields = dr.ItemArray;

                    for (var i = 0; i < fields.Length; i++)
                    {
                        if (i > 0)
                            fs.Write(options.Delimiter);

                        fs.Write(options.ValueToString(fields[i]));
                    }
                    fs.Write(StringHelper.NewLine);
                }
                fs.Close();
            }
        }

//		private static string ObjectString(CsvOptions options, object o)
//		{
//		}

        #endregion

        #region "  Constructor  "

        /// <summary>Create a CsvEngine using the specified sample file with their headers.</summary>
        /// <param name="className">The name of the record class</param>
        /// <param name="delimiter">The delimiter for each field</param>
        /// <param name="sampleFile">A sample file with a header that contains the names of the fields.</param>
        public CsvEngine(string className, char delimiter, string sampleFile) : this(new CsvOptions(className, delimiter, sampleFile))
        {
        }

        /// <summary>Create a CsvEngine using the specified number of fields.</summary>
        /// <param name="className">The name of the record class</param>
        /// <param name="delimiter">The delimiter for each field</param>
        /// <param name="numberOfFields">The number of fields of each record</param>
        public CsvEngine(string className, char delimiter, int numberOfFields) : this(new CsvOptions(className, delimiter, numberOfFields))
        {
        }

        /// <summary>Create a CsvEngine using the specified sample file with their headers.</summary>
        /// <param name="options">The options used to create the record mapping class.</param>
        public CsvEngine(CsvOptions options) : base(GetMappingClass(options))
        {
        }

        #endregion

        private static Type GetMappingClass(CsvOptions options)
        {
            var cb = new CsvClassBuilder(options);
            return cb.CreateRecordClass();
        }
    }
}

//#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions