-->

C# Read and convert dbf file to xml

2019-08-10 08:16发布

问题:

I want to read a simple foxpro dbf file and convert it into xml file and save it into my pc. Is it possible to read and convert simple file.DBF with out using any db connection?

回答1:

Yes, It is possible. Create connection on DBF table as appropriate based on this link http://www.connectionstrings.com/dbf-foxpro. Later you get the entire data onto a Dataset. You can save data set wherever you want to in XML format.



回答2:

Here is the code...

    private void btnBrowse_Click(object sender, EventArgs e)
    {
        try
        {
            var path = "F:\\Projects\\dbf"; // Path of the folder containing dbf file.
            var fileName = "Invoices1.dbf";
            var constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=DBASE III";
            var sql = "select * from " + fileName;
            var ds = new DataSet();

            using (var con = new OleDbConnection(constr))
            {
                con.Open();

                using (var cmd = new OleDbCommand(sql, con))
                {
                    using (var da = new OleDbDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        dataGridView1.DataSource = ds.Tables.Count > 0 
                                         ? ds.Tables[0].Copy() : new DataTable();
                    }
                }
            }
        }
        catch
        {
            throw;
        }
    }


标签: c# xml foxpro