Ejemplo n.º 1
1
 public ReadUsersFromFileDynamic()
 {
     var userRecordClassBuilder = new DelimitedClassBuilder("UserRecord","\t");
     userRecordClassBuilder.IgnoreFirstLines = 1;
     userRecordClassBuilder.AddField("Id", typeof(Int32));
     userRecordClassBuilder.AddField("Name", typeof(String));
     userRecordClassBuilder.AddField("Email", typeof(String));
     _tblClass = userRecordClassBuilder.CreateRecordClass();
 }
Ejemplo n.º 2
1
		public void ReadAsDataTable1()
		{

			DelimitedClassBuilder cb = new DelimitedClassBuilder("ImportContact", ","); 
			cb.IgnoreEmptyLines = true; 
			cb.GenerateProperties = true; 
			
			cb.AddField("FirstName", typeof(string)); 
			cb.LastField.TrimMode = FileHelpers.TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 

			cb.AddField("LastName", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("StreetNumber", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("StreetAddress", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("Unit", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("City", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("State", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("Zip", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 

			engine = new FileHelperEngine(cb.CreateRecordClass()); 

			string source = "Alex & Jen,Bouquet,1815,Bell Rd,, Batavia,OH,45103" + Environment.NewLine + 
							"Mark & Lisa K ,Arlinghaus,1817,Bell Rd,, Batavia,OH,45103" + Environment.NewLine +
							"Ed & Karen S ,Craycraft,1819,Bell Rd,, Batavia,OH,45103" + Environment.NewLine;

			DataTable contactData = engine.ReadStringAsDT(source);

			Assert.AreEqual(3, contactData.Rows.Count);
			Assert.AreEqual(8, contactData.Columns.Count);
			
			Assert.AreEqual("Alex & Jen", contactData.Rows[0][0].ToString());
			Assert.AreEqual("Mark & Lisa K", contactData.Rows[1][0].ToString());

			// new DelimitedClassBuilder("", ",");
		}
Ejemplo n.º 3
1
Archivo: testy.cs Proyecto: Zawulon/ETL
        private static void aaa()
        {
            string sql = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
            SqlConnection cn = new SqlConnection(sql);
            try
            {
                SqlCommand command = new SqlCommand("SELECT TOP 1 * FROM [dbo].[UnitTask];", cn);
                cn.Open();

                // get the schema for the customers table
                SqlDataReader reader = command.ExecuteReader();
                DataTable schemaTable = reader.GetSchemaTable();

                // create the FileHelpers record class
                // alternatively there is a 'FixedClassBuilder'
                DelimitedClassBuilder cb = new DelimitedClassBuilder("UnitTask", ",");
                cb.IgnoreFirstLines = 1;
                cb.IgnoreEmptyLines = true;

                // populate the fields based on the columns
                foreach (DataRow row in schemaTable.Rows)
                {
                    cb.AddField(row.Field<string>("ColumnName"), row.Field<Type>("DataType"));
                    cb.LastField.TrimMode = TrimMode.Both;
                }
                reader.Close();

                //cb.SaveToXml("");
                // load the dynamically created class into a FileHelpers engine
                FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());

                DataSet CustomersDataSet = new DataSet();
                SqlDataAdapter da_local;
                SqlCommandBuilder cmdBuilder;
                da_local = new SqlDataAdapter("SELECT * FROM [dbo].[UnitTask]", cn);
                cmdBuilder = new SqlCommandBuilder(da_local);
                da_local.Fill(CustomersDataSet, "UnitTask");
                DataTable dt = CustomersDataSet.Tables["UnitTask"];

                engine.WriteFile("c:\\UnitTask.csv", dt.Rows);
                // import your records
                //DataTable dt = engine.ReadFileAsDT("testCustomers.txt");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            finally
            {
                cn.Close();
            }
        }
Ejemplo n.º 4
1
		static void RunTimeRecords()
		{

			Console.WriteLine("Run Time Records now =) ...");
			Console.WriteLine();

			DelimitedClassBuilder cb = new DelimitedClassBuilder("Customer", ",");
			cb.AddField("CustId", typeof(Int32));
			cb.AddField("Name", typeof(string));
			cb.AddField("Balance", typeof(Decimal));
			cb.AddField("AddedDate", typeof(DateTime));
			cb.LastField.Converter.Kind = ConverterKind.Date;
			cb.LastField.Converter.Arg1 = "ddMMyyyy";
			
			
			// Estas dos lineas son el uso de la librería
			FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass());

			DataTable dt = engine.ReadFileAsDT(@"..\Data\CustomersDelimited.txt");

			// Aqui es donde ustedes agregan su código
			foreach (DataRow dr in dt.Rows)
			{
				Console.WriteLine("Customer: " + dr[0].ToString() + " - " + dr[1].ToString());
			}

		}
Ejemplo n.º 5
1
		public void LoopingFields()
		{
			DelimitedClassBuilder  cb = new DelimitedClassBuilder("MyClass", ",");
			 
			string[] lst = { "fieldOne", "fieldTwo", "fieldThree" }; 

			for (int i = 0; i < lst.Length; i++) 
			{ 
				cb.AddField(lst[i].ToString(), typeof(string)); 
			} 

			FileHelperEngine engineTemp = new FileHelperEngine(cb.CreateRecordClass()); 
		}
Ejemplo n.º 6
1
		public void FullClassBuilding()
		{
			DelimitedClassBuilder cb = new DelimitedClassBuilder("Customers", ",");
			cb.IgnoreFirstLines = 1;
			cb.IgnoreEmptyLines = true;
			
			cb.AddField("Field1", typeof(DateTime));
			cb.LastField.TrimMode = TrimMode.Both;
			cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
			cb.LastField.FieldNullValue = DateTime.Today;

			cb.AddField("Field2", typeof(string));
			cb.LastField.FieldQuoted = true;
			cb.LastField.QuoteChar = '"';

			cb.AddField("Field3", typeof(int));
			
			engine = new FileHelperEngine(cb.CreateRecordClass());

			DataTable dt = engine.ReadFileAsDT(Common.TestPath(@"Good\test2.txt"));

			Assert.AreEqual(4, dt.Rows.Count);
			Assert.AreEqual(4, engine.TotalRecords);
			Assert.AreEqual(0, engine.ErrorManager.ErrorCount);
			
			Assert.AreEqual("Hola", dt.Rows[0][1]);
			Assert.AreEqual(DateTime.Today, dt.Rows[2][0]);
			
		}
        internal static DelimitedClassBuilder LoadXmlInternal(XmlDocument document)
        {
            DelimitedClassBuilder res;
            string del = document.SelectNodes("/DelimitedClass")[0].Attributes["Delimiter"].Value;

            string className = document.SelectNodes("/DelimitedClass/ClassName")[0].InnerText;

            res = new DelimitedClassBuilder(className, del);
            return(res);
        }
Ejemplo n.º 8
0
		public void ReadAsDataTable2()
		{

			DelimitedClassBuilder cb = new DelimitedClassBuilder("ImportContact", ","); 
			cb.IgnoreEmptyLines = true; 
			cb.GenerateProperties = true; 
			
			cb.AddField("FirstName", typeof(string)); 
			cb.LastField.TrimMode = FileHelpers.TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 

			cb.AddField("LastName", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("StreetNumber", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("StreetAddress", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("Unit", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("City", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("State", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 
			
			cb.AddField("Zip", typeof(string)); 
			cb.LastField.TrimMode = TrimMode.Both; 
			cb.LastField.FieldQuoted = false; 

			engine = new FileHelperEngine(cb.CreateRecordClass()); 

			DataTable contactData = engine.ReadFileAsDT(Common.TestPath(@"Good\ReadAsDataTable.txt"));

			Assert.AreEqual(3, contactData.Rows.Count);
			Assert.AreEqual(8, contactData.Columns.Count);
			
			Assert.AreEqual("Alex & Jen", contactData.Rows[0][0].ToString());
			Assert.AreEqual("Mark & Lisa K", contactData.Rows[1][0].ToString());

			// new DelimitedClassBuilder("", ",");
		}
Ejemplo n.º 9
0
        internal static DelimitedClassBuilder LoadXmlInternal(XmlDocument document)
        {
            DelimitedClassBuilder res;
            string del = document.ChildNodes[0].Attributes[0].Value;

            string className = document.ChildNodes.Item(0).SelectNodes("/DelimitedClass/ClassName").Item(0).InnerText;

            res = new DelimitedClassBuilder(className, del);
//
//			while(reader.mReader.EOF == false)
//			{
//				reader.ReadToNextElement();
////				if (reader.mReader.LocalName == "IgnoreEmptyLines")
//			}


            return(res);
        }
Ejemplo n.º 10
0
        public DelimitedClassBuilder BuildNSECMPFile()
        {
            DelimitedClassBuilder cb = new DelimitedClassBuilder("CMPFILE", ",");

            cb.IgnoreFirstLines = 0;

            cb.AddField("Symbol", typeof(string));
            cb.AddField("Series", typeof(string));
            cb.AddField("Open", typeof(double));
            cb.AddField("High", typeof(double));
            cb.AddField("Low", typeof(double));
            cb.AddField("Close", typeof(double));
            cb.AddField("Last", typeof(double));
            cb.AddField("PrevClose", typeof(double));
            cb.AddField("Tottrdqty", typeof(int));
            cb.AddField("Tottrdval", typeof(double));
            cb.AddField("Timestamp", typeof(string));
            cb.AddField("Totaltrades", typeof(int));
            cb.AddField("Isin", typeof(string));
            cb.AddField("OI", typeof(int));
            cb.LastField.FieldNullValue = 0;

            return cb;
        }
Ejemplo n.º 11
0
        internal static DelimitedClassBuilder LoadXmlInternal(XmlDocument document)
        {
            DelimitedClassBuilder res;
            string del = document.SelectNodes("/DelimitedClass")[0].Attributes["Delimiter"].Value;

            string className = document.SelectNodes("/DelimitedClass/ClassName")[0].InnerText;

            res = new DelimitedClassBuilder(className, del);
            return res;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Loads the XML representation of a a ClassBuilder inheritor and return it. (for XML saved with SaveToXml method)
        /// </summary>
        /// <remarks>
        /// ClassBuilder inheritors: <see cref="DelimitedClassBuilder"/> or <see cref="FixedLengthClassBuilder"/>
        /// </remarks>
        /// <param name="document">The XML document with the representation of the record class.</param>
        /// <returns>A new instance of a ClassBuilder inheritor: <see cref="DelimitedClassBuilder"/> or <see cref="FixedLengthClassBuilder"/> </returns>
        public static ClassBuilder LoadFromXml(XmlDocument document)
        {
            ClassBuilder res = null;

            string classtype = document.DocumentElement.LocalName;

            if (classtype == "DelimitedClass")
            {
                res = DelimitedClassBuilder.LoadXmlInternal(document);
            }
            else
            {
                res = FixedLengthClassBuilder.LoadXmlInternal(document);
            }

            XmlNode node = document.DocumentElement["IgnoreLastLines"];

            if (node != null)
            {
                res.IgnoreLastLines = int.Parse(node.InnerText);
            }

            node = document.DocumentElement["IgnoreFirstLines"];
            if (node != null)
            {
                res.IgnoreFirstLines = int.Parse(node.InnerText);
            }

            node = document.DocumentElement["IgnoreEmptyLines"];
            if (node != null)
            {
                res.IgnoreEmptyLines = true;
            }

            node = document.DocumentElement["CommentMarker"];
            if (node != null)
            {
                res.IgnoreCommentedLines.CommentMarker = node.InnerText;
            }

            node = document.DocumentElement["CommentInAnyPlace"];
            if (node != null)
            {
                res.IgnoreCommentedLines.InAnyPlace = bool.Parse(node.InnerText.ToLower());
            }

            node            = document.DocumentElement["SealedClass"];
            res.SealedClass = node != null;

            node = document.DocumentElement["Namespace"];
            if (node != null)
            {
                res.Namespace = node.InnerText;
            }

            node = document.DocumentElement["Visibility"];
            if (node != null)
            {
                res.Visibility = (NetVisibility)Enum.Parse(typeof(NetVisibility), node.InnerText);
            }
            ;

            node = document.DocumentElement["RecordCondition"];
            if (node != null)
            {
                res.RecordCondition.Condition = (RecordCondition)Enum.Parse(typeof(RecordCondition), node.InnerText);
            }
            ;

            node = document.DocumentElement["RecordConditionSelector"];
            if (node != null)
            {
                res.RecordCondition.Selector = node.InnerText;
            }

            res.ReadClassElements(document);

            node = document.DocumentElement["Fields"];
            XmlNodeList nodes;

            if (classtype == "DelimitedClass")
            {
                nodes = node.SelectNodes("/DelimitedClass/Fields/Field");
            }
            else
            {
                nodes = node.SelectNodes("/FixedLengthClass/Fields/Field");
            }

            foreach (XmlNode n in nodes)
            {
                res.ReadField(n);
            }

            return(res);
        }
Ejemplo n.º 13
0
		public void SaveLoadXmlOptions()
		{
			DelimitedClassBuilder cbOrig = new DelimitedClassBuilder("Customers", ",");
			cbOrig.AddField("Field1", typeof(DateTime));
			cbOrig.AddField("FieldTwo", typeof(string));

			cbOrig.RecordCondition.Condition = RecordCondition.ExcludeIfMatchRegex;
			cbOrig.RecordCondition.Selector = @"\w*";

			cbOrig.IgnoreCommentedLines.CommentMarker = "//";
			cbOrig.IgnoreCommentedLines.InAnyPlace = false;

			cbOrig.IgnoreEmptyLines= true;
			cbOrig.IgnoreFirstLines = 123;
			cbOrig.IgnoreLastLines = 456;

			cbOrig.SealedClass = false;
			cbOrig.SaveToXml(@"runtime.xml");
			cbOrig = null;
			
			ClassBuilder cb2 = ClassBuilder.LoadFromXml("runtime.xml");

			Assert.AreEqual("Customers", cb2.ClassName);
			Assert.AreEqual(2, cb2.FieldCount);
			Assert.AreEqual("Field1", cb2.Fields[0].FieldName);

			Assert.AreEqual(RecordCondition.ExcludeIfMatchRegex, cb2.RecordCondition.Condition );
			Assert.AreEqual(@"\w*", cb2.RecordCondition.Selector );

			Assert.AreEqual("//", cb2.IgnoreCommentedLines.CommentMarker);
			Assert.AreEqual(false, cb2.IgnoreCommentedLines.InAnyPlace );
			Assert.AreEqual(false, cb2.SealedClass );

			Assert.AreEqual(true, cb2.IgnoreEmptyLines );
			Assert.AreEqual(123, cb2.IgnoreFirstLines );
			Assert.AreEqual(456, cb2.IgnoreLastLines );

		}
Ejemplo n.º 14
0
		public void SaveLoadXmlFileDelimited2()
		{
			DelimitedClassBuilder cb = new DelimitedClassBuilder("Customers", ",");
			cb.IgnoreFirstLines = 1;
			cb.IgnoreEmptyLines = true;
			
			cb.AddField("Field1", typeof(DateTime));
			cb.LastField.TrimMode = TrimMode.Both;
			cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
			cb.LastField.FieldNullValue = DateTime.Today;

			cb.AddField("FieldTwo", typeof(string));
			cb.LastField.FieldQuoted = true;
			cb.LastField.QuoteChar = '"';

			cb.AddField("Field333", typeof(int));

			cb.SaveToXml(@"runtime.xml");
			
			engine = new FileHelperEngine(ClassBuilder.ClassFromXmlFile("runtime.xml"));

			Assert.AreEqual("Customers", engine.RecordType.Name);
			Assert.AreEqual(3, engine.RecordType.GetFields().Length);
			Assert.AreEqual("Field1", engine.RecordType.GetFields()[0].Name);
		}
Ejemplo n.º 15
0
		public void SaveLoadXmlFileDelimited()
		{
			DelimitedClassBuilder cb = new DelimitedClassBuilder("Customers", ",");
			cb.IgnoreFirstLines = 1;
			cb.IgnoreEmptyLines = true;
			
			cb.AddField("Field1", typeof(DateTime));
			cb.LastField.TrimMode = TrimMode.Both;
			cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
			cb.LastField.FieldNullValue = DateTime.Today;

			cb.AddField("FieldTwo", typeof(string));
			cb.LastField.FieldQuoted = true;
			cb.LastField.QuoteChar = '"';

			cb.AddField("Field333", typeof(int));

			cb.SaveToXml(@"runtime.xml");
			
			DelimitedClassBuilder loaded = (DelimitedClassBuilder) ClassBuilder.LoadFromXml(@"runtime.xml");
			
			Assert.AreEqual("Field1", loaded.FieldByIndex(0).FieldName);
			Assert.AreEqual("FieldTwo", loaded.FieldByIndex(1).FieldName);
			Assert.AreEqual("Field333", loaded.FieldByIndex(2).FieldName);
			
			Assert.AreEqual("System.DateTime", loaded.FieldByIndex(0).FieldType);
			Assert.AreEqual("System.String", loaded.FieldByIndex(1).FieldType);
			Assert.AreEqual("System.Int32", loaded.FieldByIndex(2).FieldType);
			
			Assert.AreEqual(QuoteMode.AlwaysQuoted, loaded.FieldByIndex(0).QuoteMode);
			Assert.AreEqual(false, loaded.FieldByIndex(0).FieldQuoted);
			
			Assert.AreEqual('"', loaded.FieldByIndex(1).QuoteChar);
			Assert.AreEqual(true, loaded.FieldByIndex(1).FieldQuoted);
		}
Ejemplo n.º 16
0
		internal static DelimitedClassBuilder LoadXmlInternal(XmlDocument document)
		{
			DelimitedClassBuilder res;
			string del = document.ChildNodes[0].Attributes[0].Value;
			
			string className = document.ChildNodes.Item(0).SelectNodes("/DelimitedClass/ClassName").Item(0).InnerText;
			
			res = new DelimitedClassBuilder(className, del);
//			
//			while(reader.mReader.EOF == false)
//			{
//				reader.ReadToNextElement();
////				if (reader.mReader.LocalName == "IgnoreEmptyLines")
//			}
			
			
			return res;
		}