如何从 .NET 应用程序更新 .dbf 文件

发布于 2024-09-28 13:35:11 字数 1882 浏览 4 评论 0原文

我想从我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-10-05 13:35:11

我会做这样的事情:

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();

    // Insert the row
    oCmd.CommandText = @"INSERT INTO C:\rd\Setup\Test.DBF VALUES(999, 'RA-12')";
    oCmd.ExecuteNonQuery();

    // Read the table
    oCmd.CommandText = @"SELECT * FROM C:\rd\Setup\Test.DBF";

    DataTable dt = new DataTable();
    dt.Load(oCmd.ExecuteReader());

    oConn.Close();
    dataGridView1.DataSource = dt;
}

I would do something like this :

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();

    // Insert the row
    oCmd.CommandText = @"INSERT INTO C:\rd\Setup\Test.DBF VALUES(999, 'RA-12')";
    oCmd.ExecuteNonQuery();

    // Read the table
    oCmd.CommandText = @"SELECT * FROM C:\rd\Setup\Test.DBF";

    DataTable dt = new DataTable();
    dt.Load(oCmd.ExecuteReader());

    oConn.Close();
    dataGridView1.DataSource = dt;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文