/// <summary> /// 根据数据类型,进行类型转换获取数据值 /// </summary> /// <param name="fieldInfo">字段信息</param> /// <param name="text">从dbf表中获取得字符串值</param> /// <param name="buffer"></param> /// <returns></returns> private object GetValueObj(DbfFieldInfo fieldInfo, string text, byte[] buffer) { object obj; switch ((DbfFieldType)fieldInfo.FieldType) { case DbfFieldType.Character: obj = text; break; case DbfFieldType.Numeric: if (String.IsNullOrWhiteSpace(text)) { obj = 0.0; } else { obj = Convert.ToDouble(text); } break; case DbfFieldType.Date: if (String.IsNullOrWhiteSpace(text)) { obj = null; } else { obj = DateTime.ParseExact(text, "yyyyMMdd", CultureInfo.InvariantCulture); } break; case DbfFieldType.Logical: if (String.IsNullOrWhiteSpace(text)) { obj = false; } else { obj = (buffer[0] == 'Y' || buffer[0] == 'T'); } break; case DbfFieldType.General: case DbfFieldType.Byte: default: obj = buffer; break; } return(obj); }
public static Type DbfFieldType2CSharpType(DbfFieldInfo dbfFieldInfo) { Type cSharpType; switch (dbfFieldInfo.FieldType) { case DbfFieldType.Character: cSharpType = typeof(string); break; case DbfFieldType.Numeric: if (dbfFieldInfo.Accuracy == 0) { if (dbfFieldInfo.FieldLength == 4) { cSharpType = typeof(int); } else if (dbfFieldInfo.FieldLength <= 8) { cSharpType = typeof(long); } else { cSharpType = typeof(double); } } else { cSharpType = dbfFieldInfo.Accuracy > 6 ? typeof(double) : typeof(float); } break; case DbfFieldType.Date: cSharpType = typeof(DateTime); break; case DbfFieldType.Logical: cSharpType = typeof(bool); break; case DbfFieldType.General: case DbfFieldType.Byte: default: cSharpType = typeof(byte[]); break; } return(cSharpType); }
/// <summary> /// 从头中解析出字段名及字段类型 /// </summary> /// <param name="tableHeader"></param> private void GetFieldsFromHeader(byte[] tableHeader) { if (tableHeader.Length >= this.HeaderLength) { for (int i = 1; i <= this.FieldCount; i++) { var offset = i * 32; var fieldInfo = new DbfFieldInfo { FieldName = Encoding .GetString(tableHeader, offset, 10) .Replace("\0", ""), FieldType = (DbfFieldType) (Encoding .GetChars(tableHeader, offset + 11, 1)[0]), FieldLength = tableHeader[offset + 16], Accuracy = tableHeader[offset + 17] }; FieldInfos.Add(fieldInfo); } } }