我搜索了有关如何使用OLEDB或ODBC与C#/ .NET读取/写入dBase III(dbf)文件的各种技术.我已经尝试了几乎所有的tecniques,但没有成功.有人能指出我正确的方向吗?
谢谢你的时间.
就像是 ... ?
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()
来自:http://bytes.com/forum/thread112085.html
我意识到这是一个旧线程,但万一有人通过谷歌来到这里(就像我几天前)..正如我在这里写的,优雅的解决方案是使用LINQ to VFP来读取和写入DBF文件.我用一些dBase III文件测试了它.它是这样的:
您可以定义表以匹配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; } }
您可以像这样定义上下文:
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 IEntityTableMyTables { get { return this.GetTable (); } } }
您可以像这样定义上下文属性:
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 IEntityTableMyTables { get { return base.MyTables; } } }
您还需要一个连接字符串,您可以像这样在app.config中定义它(Data\
在这种情况下,相对路径用作DBF文件的源):
最后,您可以像以下一样简单地对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); }