Exemplo n.º 1
0
        /// <summary>
        /// Заполнить таблицу baseTable из файла или потока
        /// </summary>
        public void Fill <T>(BaseTable <T> baseTable, bool allowNulls)
        {
            if (baseTable == null)
            {
                throw new ArgumentNullException("baseTable");
            }

            Stream       stream = null;
            BinaryReader reader = null;

            baseTable.Items.Clear();
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

            try
            {
                stream = ioStream ?? new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                reader = new BinaryReader(stream);
                FieldDef[] fieldDefs = ReadFieldDefs(stream, reader, out int recSize);

                if (fieldDefs.Length > 0)
                {
                    // считывание строк
                    byte[] rowBuf = new byte[recSize];

                    while (stream.Position < stream.Length)
                    {
                        // загрузка данных строки таблицы в буфер для увеличения скорости работы
                        int readSize = reader.Read(rowBuf, 0, recSize);

                        // заполение строки таблицы из буфера
                        if (readSize == recSize)
                        {
                            T   item   = Activator.CreateInstance <T>();
                            int bufInd = 2;

                            foreach (FieldDef fieldDef in fieldDefs)
                            {
                                bool isNull             = fieldDef.AllowNull ? rowBuf[bufInd++] > 0 : false;
                                PropertyDescriptor prop = props[fieldDef.Name];

                                if (prop != null)
                                {
                                    object val = allowNulls && isNull ?
                                                 null : BytesToObj(rowBuf, bufInd, fieldDef.DataType);
                                    prop.SetValue(item, val);
                                }

                                bufInd += fieldDef.DataSize;
                            }

                            baseTable.AddItem(item);
                        }
                    }
                }
            }
            catch (EndOfStreamException)
            {
                // нормальная ситуация окончания файла
            }
            finally
            {
                if (fileMode)
                {
                    reader?.Close();
                    stream?.Close();
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Записать таблицу baseTable в файл или поток
 /// </summary>
 public void Update <T>(BaseTable <T> baseTable)
 {
     Update((IBaseTable)baseTable);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Fill the baseTable table from a file or stream
        /// </summary>
        public void Fill <T>(BaseTable <T> baseTable, bool allowNulls)
        {
            if (baseTable == null)
            {
                throw new ArgumentNullException(nameof(baseTable));
            }

            Stream       stream = null;
            BinaryReader reader = null;

            baseTable.Items.Clear();
            var props = TypeDescriptor.GetProperties(typeof(T));

            try {
                stream = ioStream ?? new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                reader = new BinaryReader(stream);
                FieldDef[] fieldDefs = ReadFieldDefs(stream, reader, out int recSize);

                if (fieldDefs.Length > 0)
                {
                    // read lines
                    var rowBuf = new byte[recSize];

                    while (stream.Position < stream.Length)
                    {
                        // load table row data to buffer to increase speed
                        int readSize = reader.Read(rowBuf, 0, recSize);

                        // filling the row of the table from the buffer
                        if (readSize == recSize)
                        {
                            var item   = Activator.CreateInstance <T>();
                            var bufInd = 2;

                            foreach (var fieldDef in fieldDefs)
                            {
                                bool isNull = fieldDef.AllowNull && rowBuf[bufInd++] > 0;
                                var  prop   = props[fieldDef.Name];

                                if (prop != null)
                                {
                                    var val = allowNulls && isNull
                                        ? null
                                        : BytesToObj(rowBuf, bufInd, fieldDef.DataType);
                                    prop.SetValue(item, val);
                                }

                                bufInd += fieldDef.DataSize;
                            }

                            baseTable.AddItem(item);
                        }
                    }
                }
            } catch (EndOfStreamException) {
                // normal file end situation
            } finally {
                if (fileMode)
                {
                    reader?.Close();
                    stream?.Close();
                }
            }
        }