BExIS.IO.Transform.Input.AsciiReader.ReadValuesFromFile C# (CSharp) Метод

ReadValuesFromFile() публичный Метод

Get all values from the FileStream of each variable in variable list
public ReadValuesFromFile ( Stream file, string fileName, AsciiFileReaderInfo fri, StructuredDataStructure sds, long datasetId, List variableList, int packageSize ) : List>
file Stream
fileName string name of the FileStream
fri AsciiFileReaderInfo AsciiFileReaderInfo needed
sds BExIS.Dlm.Entities.DataStructure.StructuredDataStructure StructuredDataStructure
datasetId long Id of the dataset
variableList List List of variables
packageSize int size of a package
Результат List>
        public List<List<string>> ReadValuesFromFile(Stream file, string fileName, AsciiFileReaderInfo fri, StructuredDataStructure sds, long datasetId, List<long> variableList, int packageSize)
        {
            this.FileStream = file;
            this.FileName = fileName;
            this.Info = fri;
            this.StructuredDataStructure = sds;
            this.DatasetId = datasetId;

            List<List<string>> listOfSelectedvalues = new List<List<string>>();

            // Check params
            if (this.FileStream == null)
            {
                this.ErrorMessages.Add(new Error(ErrorType.Other, "File not exist"));
            }
            if (!this.FileStream.CanRead)
            {
                this.ErrorMessages.Add(new Error(ErrorType.Other, "File is not readable"));
            }
            if (this.Info.Variables <= 0)
            {
                this.ErrorMessages.Add(new Error(ErrorType.Other, "Startrow of Variable can´t be 0"));
            }
            if (this.Info.Data <= 0)
            {
                this.ErrorMessages.Add(new Error(ErrorType.Other, "Startrow of Data can´t be 0"));
            }

            if (this.ErrorMessages.Count == 0)
            {
                Stopwatch totalTime = Stopwatch.StartNew();

                using (StreamReader streamReader = new StreamReader(file))
                {
                    string line;
                    //int index = fri.Variables;
                    int index = 1;
                    int items = 0;
                    char seperator = AsciiFileReaderInfo.GetSeperator(fri.Seperator);

                    //int end = packageSize;
                    //int start = 1;
                    // read to position
                    if (Position == 1)
                    {
                        Position = this.Info.Data;
                    }

                    Stopwatch _timer = Stopwatch.StartNew();

                    /// <summary>
                    /// go to current position is reached at the line
                    /// </summary>
                    /// <remarks></remarks>
                    for (int i = 1; i < Position; i++)
                    {
                        string l = streamReader.ReadLine();

                        if (i == this.Info.Variables)
                        {
                            VariableIdentifierRows.Add(rowToList(l, seperator));
                            convertAndAddToSubmitedVariableIdentifier();
                        }
                    }

                    // go to every line
                    while ((line = streamReader.ReadLine()) != null && items <= packageSize - 1)
                    {

                        //// is position of datastructure?
                        //if (index == this.info.Variables)
                        //{
                        //    variableIdentifierRows.Add(RowToList(line, seperator));
                        //    ConvertAndAddToSubmitedVariableIdentifier();
                        //}

                        // is position = or over startposition of data?
                        if (Position >= this.Info.Data)
                        {
                            Stopwatch rowTime = Stopwatch.StartNew();

                            // return List of VariablesValues, and error messages
                            listOfSelectedvalues.Add(GetValuesFromRow(rowToList(line, seperator), index, variableList));

                            rowTime.Stop();
                            //Debug.WriteLine("index : "+index+"   ---- Total Time of primary key check " + rowTime.Elapsed.TotalSeconds.ToString());
                        }

                        Position++;
                        index++;
                        items++;

                    }

                    _timer.Stop();

                    Debug.WriteLine(" get values for primary key check datatuples : " + _timer.Elapsed.TotalSeconds.ToString());
                }

                totalTime.Stop();
                Debug.WriteLine(" Total Time of primary key check " + totalTime.Elapsed.TotalSeconds.ToString());
            }

            return listOfSelectedvalues;
        }

Usage Example

Пример #1
0
        /// <summary>
        /// test unique of primary keys in a FileStream
        /// </summary>
        /// <remarks></remarks>
        /// <seealso cref=""/>
        /// <param name="taskManager"></param>
        /// <param name="datasetId"></param>
        /// <param name="primaryKeys"></param>
        /// <param name="ext"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsUnique(TaskManager taskManager, long datasetId, List<long> primaryKeys, string ext, string filename)
        {
            Hashtable hashtable = new Hashtable();
                Hashtable test = new Hashtable();
                List<string> testString = new List<string>();

                List<string> primaryValuesAsOneString = new List<string>();

                TaskManager TaskManager = taskManager;
                int packageSize = 1000;
                int position = 1;

                if (ext.Equals(".txt") || ext.Equals(".csv"))
                {
                    #region csv
                    do
                    {
                        primaryValuesAsOneString = new List<string>();

                        AsciiReader reader = new AsciiReader();
                        reader.Position = position;
                        Stream stream = reader.Open(TaskManager.Bus["FilePath"].ToString());

                        AsciiFileReaderInfo afri = (AsciiFileReaderInfo)TaskManager.Bus["FileReaderInfo"];

                        DataStructureManager datastructureManager = new DataStructureManager();
                        StructuredDataStructure sds = datastructureManager.StructuredDataStructureRepo.Get(Convert.ToInt64(TaskManager.Bus["DataStructureId"].ToString()));
                        // get a list of values for each row
                        // e.g.
                        // primarky keys id, name
                        // 1 [1][David]
                        // 2 [2][Javad]
                        List<List<string>> tempList = reader.ReadValuesFromFile(stream, filename, afri, sds, datasetId, primaryKeys, packageSize);

                        // convert List of Lists to list of strings
                        // 1 [1][David] = 1David
                        // 2 [2][Javad] = 2Javad
                        foreach (List<string> l in tempList)
                        {
                            string tempString = "";
                            foreach (string s in l)
                            {
                                tempString += s;
                            }
                            if (!String.IsNullOrEmpty(tempString)) primaryValuesAsOneString.Add(tempString);
                        }

                        // add all primary keys pair into the hasttable
                        foreach (string pKey in primaryValuesAsOneString)
                        {
                            if (pKey != "")
                            {

                                try
                                {
                                    hashtable.Add(Utility.ComputeKey(pKey), "pKey");
                                }
                                catch
                                {
                                    return false;
                                }
                            }

                        }

                        position = reader.Position + 1;
                        stream.Close();

                    } while (primaryValuesAsOneString.Count > 0);

                    #endregion
                }

                if (ext.Equals(".xlsm") )
                {
                    #region excel template

                    do
                    {
                        //reset
                        primaryValuesAsOneString = new List<string>();

                        ExcelReader reader = new ExcelReader();
                        reader.Position = position;
                        Stream stream = reader.Open(TaskManager.Bus["FilePath"].ToString());

                        DataStructureManager datastructureManager = new DataStructureManager();
                        StructuredDataStructure sds = datastructureManager.StructuredDataStructureRepo.Get(Convert.ToInt64(TaskManager.Bus["DataStructureId"].ToString()));
                        // get a list of values for each row
                        // e.g.
                        // primarky keys id, name
                        // 1 [1][David]
                        // 2 [2][Javad]
                        List<List<string>> tempList = reader.ReadValuesFromFile(stream, filename, sds, datasetId, primaryKeys, packageSize);

                        // convert List of Lists to list of strings
                        // 1 [1][David] = 1David
                        // 2 [2][Javad] = 2Javad
                        foreach (List<string> l in tempList)
                        {
                            string tempString = "";
                            foreach (string s in l)
                            {
                                tempString += s;
                            }
                            if (!String.IsNullOrEmpty(tempString)) primaryValuesAsOneString.Add(tempString);
                        }

                        // add all primary keys pair into the hasttable
                        foreach (string pKey in primaryValuesAsOneString)
                        {
                            if (pKey != "")
                            {

                                try
                                {
                                    hashtable.Add(Utility.ComputeKey(pKey), pKey);
                                }
                                catch
                                {
                                    stream.Close();
                                    return false;
                                }
                            }

                        }

                        position = reader.Position + 1;
                        stream.Close();

                    } while (primaryValuesAsOneString.Count > 0);

                    #endregion
                }

                return true;
        }