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(); }
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("", ","); }
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(); } }
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()); } }
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()); }
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]); }
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("", ","); }
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 ); }
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); }
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); }
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; }