문제

I am trying to open a DBF in C# file and upload it to a MySQL database. Right now I am just trying to open the DBF file, but I am getting the following error:

A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Error: Failed to retrieve the required data from the DataBase.

Unrecognized database format 'C:\Users\Path\..\..\..\SOMEFILE.DBF'.

My code is as follows.

private void button2_Click(object sender, EventArgs e)
{
    DirectoryInfo dir = new DirectoryInfo(Regex.Replace(textBox1.Text, @"\\", @"\\"));
    foreach (FileInfo file in dir.GetFiles())
    {
        MessageBox.Show(file.Name);
        string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + file.Name;
        string strAccessSelect = "SELECT * FROM "+file.Name.Substring(0,file.Name.Length-4);
        DataSet myDataSet = new DataSet();
        OleDbConnection myAccessConn = null;

        try
        {
            myAccessConn = new OleDbConnection(strAccessConn);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
            return;
        }

        try
        {
            OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
            OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

            myAccessConn.Open();
            myDataAdapter.Fill(myDataSet);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
            return;
        }
        finally
        {
            myAccessConn.Close();
        }
    }
}

I only get the first MessageBox with the file name and then it throws the error.

도움이 되었습니까?

해결책

Your connection string should identify that the OleDB data source is a DBASE type:

string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=dBase III";

Also note that when connecting to DBase via OleDB, you do not specify the DBF file, but rather the folder which contains it. The individual files are tables.

다른 팁

Use datasource without file name, only path.

string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top