如何从 .NET 应用程序更新 .dbf 文件
我想从我的 .net 应用程序更新 .dbf 文件。 我能够将 .dbf 文件读入网格,但无法从我的 .net 应用程序更新 .dbf 文件。
我使用以下代码来读取 .dbf 文件。读书没问题。但是,无法更新 .dbf 文件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Odbc;
namespace DBFwin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ConnectToDBF()
{
System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection();
oConn.ConnectionString = @"Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=D:\databases\;Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
oConn.Open();
System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
//Test.DBF is the dbf file which is located at C:\rd\Setup folder.
oCmd.CommandText = @"SELECT * FROM C:\rd\Setup\Test.DBF";
DataTable dt = new DataTable();
dt.Load(oCmd.ExecuteReader());
//Adding a row..
//DataRow dtRow = dt.LoadDataRow();
DataRow dtRow = dt.NewRow();
//FIELD1 and FIELD2 are two columns of dbf file.
dtRow["FIELD1"] = 999;
dtRow["FIELD2"] = "RA-12";
dt.BeginLoadData();
dt.Rows.Add(dtRow);
dt.EndLoadData();
//Above code adding a row in the grid, which is fine.
//How can i update dbf file from this source code???
oConn.Close();
dataGridView1.DataSource = dt;
}
private void btnClick_Click(object sender, EventArgs e)
{
ConnectToDBF();
}
}
}
注意:我还看到,第三方组件 Apollo 组件可用于从 .NET 应用程序读取、添加、编辑 .dbf 文件的记录。该组件由 VistaSoftware.com 开发。这不是免费软件。您能否确认,要更新 .dbf 文件,我是否必须使用此第三方组件,或者我可以通过我的 .net 应用程序来完成此操作。
I want to update a .dbf file from my .net application.
I was able to read the .dbf file into a grid but not able to update the .dbf file from my .net application.
I have used following code to read the .dbf file. Reading is okay. But, not able to update .dbf file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Odbc;
namespace DBFwin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ConnectToDBF()
{
System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection();
oConn.ConnectionString = @"Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=D:\databases\;Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
oConn.Open();
System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
//Test.DBF is the dbf file which is located at C:\rd\Setup folder.
oCmd.CommandText = @"SELECT * FROM C:\rd\Setup\Test.DBF";
DataTable dt = new DataTable();
dt.Load(oCmd.ExecuteReader());
//Adding a row..
//DataRow dtRow = dt.LoadDataRow();
DataRow dtRow = dt.NewRow();
//FIELD1 and FIELD2 are two columns of dbf file.
dtRow["FIELD1"] = 999;
dtRow["FIELD2"] = "RA-12";
dt.BeginLoadData();
dt.Rows.Add(dtRow);
dt.EndLoadData();
//Above code adding a row in the grid, which is fine.
//How can i update dbf file from this source code???
oConn.Close();
dataGridView1.DataSource = dt;
}
private void btnClick_Click(object sender, EventArgs e)
{
ConnectToDBF();
}
}
}
NOTE: I have also seen that, a third party component Apollo component is availble to read, add, edit records of .dbf file from .NET application. This component is developed by VistaSoftware.com. This not a freeware. Could you please confirm, to update .dbf file whether i have to use this third party component or i can do it by my .net application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会做这样的事情:
I would do something like this :