Search code examples
vb.netoledbfoxpro

Cannot connect to dbf file


I'm trying to connect to to a foxpro table (.dbf) from a test vb.net form. I recieve an OleDbException with the message "Cannot open file c:\emp\emptbl.dbf"

Have tried with both of the following connection strings:

Provider=VFPOLEDB.1;Data Source=C:\emp\emptbl.dbf

from the MSDN article here

Provider=vfpoledb;Data Source=C:\emp\emptbl.dbf;Collating Sequence=machine;

from connectionstrings.com

The latter seems to be the type to use when connecting to a single table, but the same exception is thrown regadless of which is used.

I can open and perform the same query okay in visual foxpro 6.0. Here's my code:

    Dim tbl As DataTable = New DataTable()

        Using con = New OleDbConnection(conString)
            cmd = New OleDbCommand() With {.Connection = con, .CommandType = CommandType.Text}
            Dim sSQL As String = "SELECT * FROM(EMPTBL)"
            cmd.CommandText = sSQL
            Dim adp As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            con.Open()
            adp.Fill(ds)
            con.Close()
            If (ds.Tables.Count > 0) Then
                tbl = ds.Tables(0)
            End If
        End Using

    Return tbl

Solution

  • The OleDB provider should only connect to the PATH where the tables are... not the actual file name. Once you connect to the PATH, you can query from ANY .Dbf file that is located in it

    Provider=VFPOLEDB.1;Data Source=C:\emp

    select * from emptbl where ...

    You can also look at other connection string settings at

    ConnectionStrings.com

    UPDATE per comment.

    It appears you are getting closer. with your attempt without the () parens. In VFP, within parens, it interprets that as "look for a variable called EMPTBL", and from its value is the name of the table to query from. Not something you would need to apply via OleDB connection.

    Now, cant open the file is another. Is it POSSIBLE that another application has the table open and the file is in exclusive use? and thus can not be opened by the .net app too? Even for grins, if you open VFP, and just do a simple create table in the C:\Emp folder and put a single record in it, then you know no other program will be using it as it is a new file. Quit out of VFP and try to query THAT table. There should be no locks, no other program is expecting it, so it should never be opened by anything else and you should be good to go.