最佳的方式从C#处理的.dbf [英] Optimal way to handle .dbf from C#

查看:1217
本文介绍了最佳的方式从C#处理的.dbf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用哪些数据提供商从C#更新.dbf文件?

What data-provider can I use to update .dbf file from C#?

我尝试了几种不同的.dbf供应商创建一个数据源,但我得到的消息是这样的: 错误信息:错误HYC00微软ODBC dBase驱动程序可选功能未实现

I tried a few different .dbf providers to create a DataSource but I get a message like this: "Error Message: ERROR HYC00 Microsoft ODBC dBase Driver Optional feature not implemented."

或者当我生成一个数据集和一个DataAdapter与更新功能我得到的答案:更新,需要时通过的DataRow集合与已修改行有效的UpdateCommand

Or when I generated a dataset and a dataadapter with an update function I got: "Update requires a valid UpdateCommand when passed DataRow collection with modified rows."

如果有谁知道一些方法来有很多的更新,请帮助工作的.dbf从C#。当我试图通过一个更新的行之一,它是速度太慢,因为供应商将失去太多的时间在寻找一个大的.dbf文件。也许有一种方法可以自动建立索引,并且数据源知道使用它?

If anyone knows some way to work on .dbf from C# with lots of updates please help. When I try to update rows one by one it is too slow because the provider will lose too much time on searching a big .dbf file. Maybe there is a way to automatically build an index and that the data-source knows to use it?

另一种方法是加载所有到类似的数据集和更新后的所有更改完成,但更新部分没有工作了。

Another way is to load all into something like dataset and update after all changes are done, but the update part is not working for now.

请帮助!

推荐答案

您可以使用 LINQ到VFP 读取和写入DBF文件。我用它来编辑一些的dBase III文件,就像魅力。

You can use LINQ to VFP to read from and write to DBF files. I use it to edit some dBase III files, works like charm.

您定义你的表像这样的DBF定义相匹配:

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

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; }
}

您这样定义的背景下:

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; }
    }
}

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

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):

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

最后,您可以执行读取和写入,并从DBF文件那样简单:

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

// 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);
}

这篇关于最佳的方式从C#处理的.dbf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆