Search code examples
asp.netconnection-stringdbase

Connecting to a dBase file on a remote server


I have an asp.net website that needs to connect to a dBase file on a remote server. The remote server has a ODBC System DSN connection configured but I have no idea how to connect to it.


Solution

  • The ODBC connection on the server won't help you. The ODBC Connection needs to be set up on the machine you want to connect FROM, not the one you want to connect TO.

    In order to connect to the DBASE files (and treat them like a database) you will need to

    1. Map a drive so that you can access the location of the files..
    2. Connect use the OleDbConnection.

    It ALSO deals with a problem you will have reading DBase files from .NET. If you read them often enough, the app will start throwing a "System.Resources.Exceeded" exception. The only reliable solution I've found has been to kill the app and restart it, which is done in the code named FixMyself. (Not included as it contains sensitive data). The FixMyself routine essentially starts a second exe that kills THIS exe and then restarts it.

    The sample code below is copied from production code, and should give you a push int he right direction. it maps the drive, connects, and reads.

    It's ugly but it works. It's also only partial as it calls several functions not included here. But again, it should be enough to get you going.

      Public Function GetRegisterConnectionString(ByVal PathToFolder As String)
            Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & PathToFolder & ";Extended Properties=dBASE IV;User ID=Admin;Password="
        End Function
        Public Sub ReadMyDbaseFile(ByVal DriveLetter As String, ByVal IPAddress As String)
    
            Dim DpalmPath As String = "\\" & IPAddress & "\c$\Dpalm"
            Dim cn As New System.Data.OleDb.OleDbConnection("")
            cn.ConnectionString = Me.GetRegisterConnectionString(DpalmPath)
            If ds.Tables.Contains("CurrentPrices") Then
                ds.Tables.Remove("CurrentPrices")
            End If
    
        Dim POSAdapter As New System.Data.OleDb.OleDbDataAdapter("select * From MyDbaseFile WHERE SomeField > 0 AND ACTIVE = -1", cn)
    
        Try
            POSAdapter.Fill(ds, "CurrentPrices")
    
        Catch ex As Exception
            If InStr(ex.ToString().ToLower(), "system resource exceeded") Then
                WriteToLog("System Resource Exceeded Error was thrown on register " & DriveLetter & ", IP " & IPAddress & ".")
                Me.FixMyself()
            Else
                Throw New Exception(ex.ToString())
            End If
        End Try
        ds.Tables("CurrentPrices").Columns.Add("LastModified", GetType(Date))
        POSAdapter.Dispose()
        POSAdapter = Nothing
        cn.Dispose()
        cn = Nothing
        ds.AcceptChanges()
    
        GC.Collect()
    
    
    End Sub