【问题标题】:C# Read from .DBF files into a datatableC# 从 .DBF 文件读入数据表
【发布时间】:2014-04-17 04:10:39
【问题描述】:

我需要使用 C# 连接到 Visual Studio 中的 .dbf 文件并填充数据表。有任何想法吗?我目前可以在 Visual Fox Pro 9.0 中查看表格

我尝试过但失败的代码,继续获取

外部表不是预期的格式。

private OleDbConnection conn;
private OleDbCommand cmd;
private OleDbDataReader dr;
private string sqlStr = "";
private DataSet myDataSet;
private OleDbDataAdapter myAdapter;


void test2()
{
    conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\PC1\Documents\\Visual FoxPro Projects\\;Extended Properties=DBASE IV;");
    conn.Open();
    sqlStr = "Select * from Clients.dbf";
    //Make a DataSet object
    myDataSet = new DataSet();
    //Using the OleDbDataAdapter execute the query
    myAdapter = new OleDbDataAdapter(sqlStr, conn);
    //Build the Update and Delete SQL Statements
    OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdapter);         

    //Fill the DataSet with the Table 'bookstock'
    myAdapter.Fill(myDataSet, "somename");
    // Get  a FileStream object
    FileStream myFs = new FileStream
          ("myXmlData.xml", FileMode.OpenOrCreate, FileAccess.Write);
    // Use the WriteXml method of DataSet object to write XML file from the   DataSet
    //  myDs.WriteXml(myFs);
    myFs.Close();
    conn.Close();
}

【问题讨论】:

  • 数据表或数据集
  • 有很多方法,但你需要展示你研究/尝试过的东西。您是否下载了 VFP OleDB 提供程序?调查过 OleDbConnection、OleDbCommand?
  • 我已添加我的代码,但不断收到该错误。有什么想法吗?
  • 由于其他人认为过于宽泛,但显然没有使用 VFP 的经验,因此建议搁置……见鬼。看看这个链接stackoverflow.com/questions/4107067/… 作为一个很好的起点来查询和获取正确的 VFP .net 提供程序......然后,查看关于 VFP 和参数化查询的其他答案

标签: c# datatable visual-foxpro dbf


【解决方案1】:

这段代码对我有用!

public DataTable GetYourData()
    {
        DataTable YourResultSet = new DataTable();

        OleDbConnection yourConnectionHandler = new OleDbConnection(
            @"Provider=VFPOLEDB.1;Data Source=C:\Users\PC1\Documents\Visual FoxPro Projects\");

        // if including the full dbc (database container) reference, just tack that on
        //      OleDbConnection yourConnectionHandler = new OleDbConnection(
        //          "Provider=VFPOLEDB.1;Data Source=C:\\SomePath\\NameOfYour.dbc;" );


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

        if (yourConnectionHandler.State == ConnectionState.Open)
        {
            string mySQL = "select * from CLIENTS";  // dbf table name

            OleDbCommand MyQuery = new OleDbCommand(mySQL, yourConnectionHandler);
            OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);

            DA.Fill(YourResultSet);

            yourConnectionHandler.Close();
        }

        return YourResultSet;
    }

【讨论】:

  • 这需要预先创建表吗?如果 DBF 文件发生变化,我希望在读取文件后立即创建表
【解决方案2】:

Visual FoxPro DBF 不是 dBase IV DBF,因此大多数版本的 Microsoft Access 的 Jet 数据库引擎都无法读取。 (MSDN 有一些 specifics,如果你在乎的话。)

您需要将 DBF 从 FoxPro 导出为实际的 dBase 格式,或者您需要让 C# 使用 Visual FoxPro OLEDB 提供程序打开它。

安装提供程序后,您需要将连接字符串的“Provider”参数更改为以下内容,假设您的 DBF 位于该文件夹中。

Provider=VFPOLEDB.1;Data Source=C:\Users\PC1\Documents\Visual FoxPro Projects\;

(使用@"" 字符串格式;在代码示例中,PC1 和 Documents 之间遗漏了一个斜杠。)

【讨论】:

  • 我在答案中添加了,请您帮忙
  • 谢谢你的连接字符串帮助我做对了。
  • 看来您实际上并没有安装提供程序。
猜你喜欢
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
相关资源
最近更新 更多