【问题标题】:Accessing legacy Visual FoxPro database from C#从 C# 访问旧版 Visual FoxPro 数据库
【发布时间】:2021-06-25 20:45:43
【问题描述】:

我有一个旧版 Visual FoxPro 数据库,我需要从中获取数据。 我的数据库文件夹中有 DBC、DCT、DCX 和 FPT 文件。

我安装了 Visual FoxPro 提供程序和驱动程序并编写了一个简短的 C# 程序来访问数据。 我还确保程序在 32 位模式下运行,因为我知道 VFP 驱动程序是 32 位的。 但是我收到以下错误:

附加信息:连接错误:[Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序

我确保在连接字符串中指定了 Windows ODBC 32 位管理工具中显示的驱动程序名称(“Microsoft Visual FoxPro 驱动程序”),但它似乎没有帮助。

代码:

OleDbConnection yourConnectionHandler = new OleDbConnection(
 @"Provider=VFPOLEDB.1;DRIVER=Microsoft Visual FoxPro Driver;Data Source=mydatabase.dbc;Mode=Read;Collating Sequence=machine");

// Open the connection, and if open successfully, you can try to query it
yourConnectionHandler.Open();

if (yourConnectionHandler.State == ConnectionState.Open)
{
    string mySQL = "select * from tablename";

    var command = yourConnectionHandler.CreateCommand();
    command.CommandText = mySQL;
    var res = command.ExecuteReader(); // throws the error

    yourConnectionHandler.Close();
}

我已经被这个问题困扰了很长时间,并且已经尝试了我在网上可以找到的所有内容。 有什么想法吗?

【问题讨论】:

  • Possibly useful。也许尝试 .dbf 替代方案
  • ...odbc 标签也不是 oledb
  • @MickyD - 现有的表文件是 DBC/DCT 格式,所以我没有 DBF 格式。
  • DBC是数据库容器,DBF在里面,应该和DBC在同一个文件夹中
  • 您的连接字符串需要指向 DBC 中的特定 DBF。

标签: c# oledb visual-foxpro


【解决方案1】:

您说的是提供者并尝试使用 OleDbConnection(这是正确的),但在连接字符串中定义了一个 ODBC 驱动程序。也就是说,你的连接字符串是错误的。

using (OleDbConnection cn = new OleDbConnection(
 @"Provider=VFPOLEDB;Data Source=c:\MyPath\mydatabase.dbc;Mode=Read;Collating Sequence=machine"))
using (OleDbCommand cmd = new OleDbCommand("select * from tablename", cn))
{
    cn.Open();
    var reader = cmd.ExecuteReader(); 
    // do something with the reader. ie:
    // someDataTable.Load(reader);
    cn.Close();
}

效果非常好。在connectionstring中不需要使用数据库名,直接使用表的路径作为数据源即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多