我正在以这种方式使用OleDb读取DBF文件:
[TestMethod]
public void TestMethod2()
{
const string path = @"D:\VL816183.DBF";
var connection = new OleDbConnection(string.Format("Provider=Microsoft.Jet.Oledb.4.0;Data Source={0};Extended Properties=\"dBase IV\"", Path.GetDirectoryName(path)));
connection.Open();
var command = new OleDbCommand(string.Format("select MNO from {0}", Path.GetFileName(path)), connection);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var str = (string)reader["MNO"];
}
}
connection.Close();
}
一切似乎都还可以,但是字符串数据有问题.源数据库包含用CodePage = 852保存的字符串,我找不到正确读取它的方法.
我试图将CharSet / CodePage / CharacterSet设置为连接字符串的扩展属性,但是没有任何运气(实际上,抛出了异常:找不到可安装的ISAM).
我也尝试过使用’vfpoledb’提供程序来阅读它,但还是没有运气.
例如,有字符串“FRANTIŠEK”,但str变量包含“ FRANTIµEK”.
有人知道怎么做吗?
谢谢
解决方法:
好吧,几个小时后,我设法以适当的方式获取字符串.
诀窍是将字符串列读取为varbinary(length):
[TestMethod]
public void TestMethod2()
{
const string path = @"D:\KN_Vzorka_2012\VL816183.DBF";
var connection = new OleDbConnection(string.Format("Provider=vfpoledb;Data Source={0};Extended Properties=\"dBase IV\";Locale Identifier=852;", Path.GetDirectoryName(path)));
connection.Open();
var command = new OleDbCommand(string.Format("select cast(MNO as varbinary(20)) as MNO FROM {0}", Path.GetFileName(path)), connection);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var arr = (byte[])reader["MNO"];
var str = Encoding.GetEncoding(852).GetString(arr);
}
}
connection.Close();
}
唯一的问题是varbinary CAST内部的长度.但这有效.希望这也会对某人有所帮助.