OleDB: DBF type mapping
I'm trying to read a dbf file. Before I read data in it, I analyze its schema table in order not to read a wrong file. When I analyze it, I check its column names and their types. My problem is that on some systems a particular field maps to System.Double, but on the other it maps to System.Int16. In fact, the actual type of that field (as seen in DBFNavigator) is numeric(4,0). I wrote a test app to check those dbf files on the client`s side. This is the class that outputs the dbf schema:
public class DBFAnalyzerCore:IDisposable
{
private OleDbConnection _connection;
private bool _keyInfo;
private string _fileName;
bool _bDisposed;
public DBFAnalyzerCore(string path, bool keyInfo)
{
_connection = new OleDbConnection(
String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=dBASE IV;User ID=Admin;Password=",
System.IO.Path.GetDirectoryName(path)));
_connection.Open();
_keyInfo = keyInfo;
_fileName = System.IO.Path.GetFileName(path);
}
public void Analyze(StringBuilder result)
{
OleDbCommand command = null;
IDataReader reader = null;
try
{
command = new OleDbCommand(String.Format("SELECT * FROM [{0}]", _fileName), _connection);
if (_keyInfo)
reader = command.ExecuteReader(CommandBehavior.KeyInfo);
else
reader = command.ExecuteReader();
DataTable schema = reader.GetSchemaTable();
OutputShema(schema, result);
}
finally
{
if (reader != null) reader.Close();
if (command != null) command.Dispose();
}
}
private void OutputShema(DataTable schema, StringBuilder sb)
{
foreach (DataRow row in schema.Rows)
{
sb.AppendLine(String.Format("{0} - {1}", row["ColumnName"], row["DataType"]));
}
}
protected virtual void Dispose(bool bDisposing)
{
if (bDisposing)
{
_connection.Dispose();
_开发者_开发百科bDisposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
As a result on the client's system it showed System.Int16. The system is Win7 x64. HKEY_LOCAL_MACHINE\Software\Microsoft\DataAccess\Version is "6.1.7601.17514". The same is mine but I get System.Double! At the same time on a clients`s "new" system it showed System.Double. What kind of tweaks/settings could have caused this? I understand that now I should remove column type checking, but due to our versioning policy I can't do that right now.
精彩评论