我正在以这种方式使用 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 中的长度。但它有效。希望这也能帮助别人。
关于c# - ADO.NET OleDB 和非常古老的 dBASE IV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15640373/