如何使用 C#/.NET ODBC 或 OLE 读取/写入 dBase III 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/75705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:12:59  来源:igfitidea点击:

How to read/write dBase III files using C#/.NET ODBC or OLE?

提问by JP Richardson

I have searched for various techniques on how to read/write dBase III (dbf) files using OLEDB or ODBC with C#/.NET. I have tried almost all of the tecniques posted, but without success. Can someone point me in the right direction?

我搜索了有关如何使用 OLEDB 或 ODBC 和 C#/.NET 读/写 dBase III (dbf) 文件的各种技术。我已经尝试了几乎所有发布的技术,但没有成功。有人可以指出我正确的方向吗?

Thanks for your time.

谢谢你的时间。

回答by Kearns

FoxPro 2.0 files were exactly the same as dBase III files with an extra bit for any field that was of type "memo" (not sure the exact name, it's been a while). That means that if you just use a FoxPro 2.x methodfor accessing the files, it should work.

FoxPro 2.0 文件与 dBase III 文件完全相同,对于任何类型为“备忘录”的字段(不确定确切名称,已经有一段时间了)都有一个额外的位。这意味着如果您只使用FoxPro 2.x 方法来访问文件,它应该可以工作。

回答by Fionnuala

Something like ... ?

就像是 ... ?

 ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\dBase;Extended Properties=dBase III"
Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(ConnectionString )
dBaseConnection.Open()

From: http://bytes.com/forum/thread112085.html

来自:http: //bytes.com/forum/thread112085.html

回答by Fionnuala

This is a nice aproach, i didn't tested, but i will soon...

这是一个很好的方法,我没有测试过,但我很快就会......

http://www.c-sharpcorner.com/uploadfile/rfederico/xbaseenginerfv12022005011623am/xbaseenginerfv.aspx

http://www.c-sharpcorner.com/uploadfile/rfederico/xbaseenginerfv12022005011623am/xbaseenginerfv.aspx

回答by Dejan Janju?evi?

I realize this is an old thread, but in case someone gets here by google (like I have few days ago).. As I wrote here, the elegant solution is to use LINQ to VFPto read from and write to DBF files. I tested it with some dBase III files. It goes like this:

我意识到这是一个旧线程,但万一有人通过谷歌到达这里(就像我几天前一样)。正如我在这里写的那样,优雅的解决方案是使用LINQ to VFP来读取和写入 DBF 文件。我用一些 dBase III 文件对其进行了测试。它是这样的:

You define your table to match the DBF definition like this:

您定义表以匹配 DBF 定义,如下所示:

public partial class MyTable 
{
    public System.Int32 ID { get; set; }
    public System.Decimal Field1 { get; set; }
    public System.String Field2 { get; set; }
    public System.String Field3 { get; set; }
}

You define the context like this:

您可以像这样定义上下文:

public partial class Context : DbEntityContextBase 
{
    public Context(string connectionString)
        : this(connectionString, typeof(ContextAttributes).FullName) 
    {
    }

    public Context(string connectionString, string mappingId)
        : this(VfpQueryProvider.Create(connectionString, mappingId)) 
    {
    }

    public Context(VfpQueryProvider provider)
        : base(provider) 
    {
    }

    public virtual IEntityTable<MyTable> MyTables 
    {
        get { return this.GetTable<MyTable>(); }
    }
}

You define context attributes like this:

您可以像这样定义上下文属性:

public partial class ContextAttributes : Context 
{
    public ContextAttributes(string connectionString)
        : base(connectionString) {
    }

    [Table(Name="mytable")]
    [Column(Member="ID", IsPrimaryKey=true)]
    [Column(Member="Field1")]
    [Column(Member="Field2")]
    [Column(Member="Field3")]
    public override IEntityTable<MyTable> MyTables 
    {
        get { return base.MyTables; }
    }
}

You also need a connection string, you can define it in app.config like this (Data\relative path is used as the source of DBF files in this case):

您还需要一个连接字符串,您可以像这样在 app.config 中定义它(Data\在这种情况下使用相对路径作为 DBF 文件的来源):

<connectionStrings>
  <add name="VfpData" providerName="System.Data.OleDb"
    connectionString="Provider=VFPOLEDB.1;Data Source=Data\;"/>
</connectionStrings>

And finally, you can perform reading and writing to and from DBF files as simple as:

最后,您可以像这样简单地对 DBF 文件执行读写操作:

// Construct a new context
var context = new Context(ConfigurationManager.ConnectionStrings["VfpData"].ConnectionString);

// Write to MyTable.dbf
var my = new MyTable
{
    ID = 1,
    Field1 = 10,
    Field2 = "foo",
    Field3 = "bar"
}
context.MyTables.Insert(my);

// Read from MyTable.dbf
Console.WriteLine("Count:  " + context.MyTables.Count());
foreach (var o in context.MyTables)
{
    Console.WriteLine(o.Field2 + " " + o.Field3);
}

回答by DRapp

I have offered many answers on working with database files (more specifically VFP, but the Microsoft VFP OleDb provider will recognize older dbase files. You can do a search to find more of these links via:

我已经提供了许多有关使用数据库文件的答案(更具体地说是 VFP,但 Microsoft VFP OleDb 提供程序会识别较旧的 dbase 文件。您可以通过以下方式进行搜索以找到更多这些链接:

user:74195[vfp][oledb]

用户:74195[vfp][oledb]

First, I would start with getting the Microsoft VFP OleDb Providerdownload.

首先,我将从获取Microsoft VFP OleDb Provider下载开始。

Next, if you already have some dbf files you are trying to connect to for testing, you need to establish a connection. The connection must point to the PATH where the files are located, not the specific .dbf file. So, if you have a folder with 20 tables in it, once you connect to the PATH, you can query from any/all the tables via standard VFP-SQL Syntax (common with many sql the overall structure, but different based on some functions like string, date and number manipulations).

接下来,如果您已经有一些 dbf 文件要尝试连接以进行测试,则需要建立连接。连接必须指向文件所在的 PATH,而不是特定的 .dbf 文件。因此,如果您有一个包含 20 个表的文件夹,一旦您连接到 PATH,您就可以通过标准的 VFP-SQL 语法从任何/所有表中进行查询(与许多 sql 的整体结构相同,但根据某些功能而有所不同)如字符串、日期和数字操作)。

Learn about PARAMETERIZING your queries. With VFP OleDb, parameters are done with the "?" character as a place-holder, so the parameters need to be added in the exact same sequence as they appear in the query. The "?" can appear as field values, join conditions, where criteria, etc.

了解参数化您的查询。使用 VFP OleDb,参数是用“?”完成的。字符作为占位符,因此需要以与它们在查询中出现的完全相同的顺序添加参数。这 ”?” 可以显示为字段值、连接条件、where 条件等。

The following are a FEW to get you started to HOPEFULLY get you started with a valid connection, query, then insert/update/delete with parameters.

以下是一些帮助您开始希望有效连接、查询,然后插入/更新/删除参数的内容。

  1. Sample showing a connection string and simple query from a table

  2. Shows a parameterized sql-insertbut in this case gets the data from another data source, such as sql-server and creating a VFP/dbf style table from it. It goes through cycling through records and pulling values for each parameter and inserting.

  3. and another showing parameterized SQL-update

  1. 显示连接字符串和来自表的简单查询的示例

  2. 显示参数化的 sql-insert但在这种情况下从另一个数据源获取数据,例如 sql-server 并从中创建 VFP/dbf 样式表。它通过循环记录和提取每个参数的值并插入。

  3. 另一个显示参数化的 SQL 更新

Good luck, and there are plenty of others who answer on VFP and OleDb Access, these are just some that I have specifically participated in and show functional implementations that may have something you may otherwise may have missed.

祝你好运,还有很多其他人回答了 VFP 和 OleDb Access,这些只是我专门参与的一些,并展示了可能有一些你可能会错过的功能实现。