Пример #1
0
        /// <summary>
        /// Routine to copy data from a delimited data text file to an ADO.NET DataTable object.
        /// </summary>
        /// <param name="dataFileName">Path to file containing the data.</param>
        /// <param name="lineDefFileName">Path to file containing the PFDelimitedDataLine object that encapsulates the line formatting information for the specified data file.</param>
        /// <returns>ADO.NET DataTable.</returns>
        public DataTable ImportDelimitedTextFileToDataTable(string dataFileName, string lineDefFileName)
        {
            PFDelimitedDataLine lineDef = PFDelimitedDataLine.LoadFromXmlFile(lineDefFileName);

            return(ImportDelimitedTextFileToDataTable(dataFileName, lineDef));
        }
Пример #2
0
        public static void DelimitedLineTextFileTests(int numRowsToOutput)
        {
            PFTextFile          textFile = new PFTextFile(@"c:\temp\DelimitedText.txt", PFFileOpenOperation.OpenFileForWrite);
            PFDelimitedDataLine line     = new PFDelimitedDataLine(5);
            RandomString        dat      = new RandomString();
            RandomNumber        siz      = new RandomNumber();
            int    num = 0;
            string str = string.Empty;

            try
            {
                line.LineTerminator  = "\r\n";
                line.ColumnSeparator = ",";
                line.SetColumnDefinition(0, "FirstColumn", 10);
                line.SetColumnDefinition(1, "SecondColumn", 15);
                line.SetColumnDefinition(2, "ColumnThree", 5);
                line.SetColumnDefinition(3, "ColumnFour", 35);
                line.SetColumnDefinition(4, "FifthColumn", 25);

                textFile.WriteData(line.OutputColumnNames());

                for (int rowNum = 0; rowNum < numRowsToOutput; rowNum++)
                {
                    for (int inx = 0; inx < 5; inx++)
                    {
                        num = siz.GenerateRandomInt(1, line.ColumnDefinitions.ColumnDefinition[inx].ColumnLength);
                        str = dat.GetStringAL(num);
                        line.ColumnData.ColumnDataValue[inx].Data = str;
                    }
                    textFile.WriteData(line.OutputColumnData());
                }

                textFile.CloseFile();

                line.SaveToXmlFile(@"c:\temp\DelimitedText.xml");

                //read textfile
                textFile.OpenFile(@"c:\temp\DelimitedText.txt", PFFileOpenOperation.OpenFileToRead);
                string input = textFile.ReadLine();
                line.VerifyColumnNames(input);
                Program._messageLog.WriteLine(line.ColumnDefinitions.ToString());
                input = textFile.ReadLine();
                line.ParseData(input);
                Program._messageLog.WriteLine(line.ColumnData.ToString());

                textFile.CloseFile();


                //double check loading of xml schema works
                PFDelimitedDataLine line2 = PFDelimitedDataLine.LoadFromXmlFile(@"c:\temp\DelimitedText.xml");

                Program._messageLog.WriteLine(line2.ToXmlString());

                PFTextFile textFile2 = new PFTextFile(@"c:\temp\DelimitedText2.txt", PFFileOpenOperation.OpenFileForWrite);
                char[]     tst       = { 'a', 'b', 'c', 'd', 'e' };

                //line2.LineTerminator = "\r\n";
                line2.ColumnSeparator = ",";
                line2.SetColumnDefinition(0, "Column One", 10);
                line2.SetColumnDefinition(1, "2ndColumn", 15);
                line2.SetColumnDefinition(2, "Column Three", 5);
                line2.SetColumnDefinition(3, "Column Four", 35);
                line2.SetColumnDefinition(4, "5thColumn", 25);

                textFile2.WriteData(line2.OutputColumnNames());

                for (int rowNum = 0; rowNum < numRowsToOutput; rowNum++)
                {
                    for (int inx = 0; inx < 5; inx++)
                    {
                        num = siz.GenerateRandomInt(1, line2.ColumnDefinitions.ColumnDefinition[inx].ColumnLength);
                        str = new string(tst[inx], num);
                        line2.ColumnData.ColumnDataValue[inx].Data = str;
                    }
                    textFile2.WriteData(line2.OutputColumnData());
                }

                textFile2.CloseFile();
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                if (textFile != null)
                {
                    textFile = null;
                }
                ;
            }
        }//end DelimitedLineTextFileTests