MapAround.IO.DbaseFieldDescriptor.GetDataType C# (CSharp) Method

GetDataType() public static method

Gets a CLR type by the character defining a dBase field type.
public static GetDataType ( char DbaseType ) : Type
DbaseType char
return System.Type
		public static Type GetDataType(char DbaseType)
        {
            Type type;
            switch (DbaseType)
            {
                case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
                    type = typeof(bool);
                    break;
                case 'C': // char or string
                    type = typeof(string);
                    break;
                case 'D': // date
                    type = typeof(DateTime);
                    break;
                case 'N': // numeric
                    type = typeof(double);
                    break;
                case 'F': // double
                    type = typeof(float);
                    break;
                case 'B': // BLOB - not a dbase but this will hold the WKB for a geometry object.
                    type = typeof(byte[]);
                    break;
                default:
                    throw new NotSupportedException("Do not know how to parse Field type " + DbaseType);
            }
            return type;
        }
	}

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Reads a dBase header.
        /// </summary>
        /// <param name="reader">A System.IO.BinaryReader instance to read header</param>
        public void Read(BinaryReader reader)
        {
            // type of reader.
            _fileType = reader.ReadByte();
            if (_fileType != 0x03)
            {
                throw new NotSupportedException("Unsupported DBF Type " + _fileType);
            }

            // parse the update date information.
            int year        = (int)reader.ReadByte();
            int month       = (int)reader.ReadByte();
            int day         = (int)reader.ReadByte();
            int yearShifted = year + 1900;

            try
            {
                _updateDate = new DateTime(yearShifted, month, day);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Debug.WriteLine("Data reading failed.");
                _updateDate = DateTime.MinValue;
            }

            // read the number of records.
            _numRecords = reader.ReadInt32();

            // read the length of the header structure.
            _headerLength = reader.ReadInt16();

            // read the length of a record
            _recordLength = reader.ReadInt16();

            // skip the reserved bytes in the header.
            //in.skipBytes(20);
            //reader.ReadBytes(20);
            reader.BaseStream.Seek(29, SeekOrigin.Begin);

            //языковой драйвер
            _encoding = getDbaseLanguageDriver(reader.ReadByte());

            reader.BaseStream.Seek(32, SeekOrigin.Begin);

            // calculate the number of Fields in the header
            _numFields = (_headerLength - _fileDescriptorSize - 1) / _fileDescriptorSize;

            // read all of the header records
            _dbaseColumns = new DbaseFieldDescriptor[_numFields];

            for (int i = 0; i < _numFields; i++)
            {
                _dbaseColumns[i] = new DbaseFieldDescriptor();

                // read the field name
                byte[] buffer = reader.ReadBytes(11);
                string name   = _encoding.GetString(buffer);

                if (name.Contains("\0"))
                {
                    name = name.Substring(0, name.IndexOf('\0'));
                }

                name = name.Replace("\0", "").Trim();

                int nullPoint = name.IndexOf((char)0);
                if (nullPoint != -1)
                {
                    name = name.Substring(0, nullPoint);
                }
                _dbaseColumns[i].Name = name;

                // read the field type
                _dbaseColumns[i].DbaseType = (char)reader.ReadByte();
                _dbaseColumns[i].DataType  = DbaseFieldDescriptor.GetDataType(_dbaseColumns[i].DbaseType);

                // read the field data address, offset from the start of the record.
                _dbaseColumns[i].DataAddress = reader.ReadInt32();

                // read the field length in bytes
                int tempLength = (int)reader.ReadByte();
                if (tempLength < 0)
                {
                    tempLength = tempLength + 256;
                }
                _dbaseColumns[i].Length = tempLength;

                // read the field decimal count in bytes
                _dbaseColumns[i].DecimalCount = (int)reader.ReadByte();
                if (_dbaseColumns[i].DecimalCount == 0 && _dbaseColumns[i].DataType == typeof(double))
                {
                    if (_dbaseColumns[i].Length <= 2)
                    {
                        _dbaseColumns[i].DataType = typeof(Int16);
                    }
                    else if (_dbaseColumns[i].Length <= 4)
                    {
                        _dbaseColumns[i].DataType = typeof(Int32);
                    }
                    else
                    {
                        _dbaseColumns[i].DataType = typeof(Int64);
                    }
                }

                // read the reserved bytes.
                //reader.skipBytes(14);
                reader.ReadBytes(14);
            }

            // Last byte is a marker for the end of the field definitions.
            reader.ReadBytes(1);
        }
DbaseFieldDescriptor