0

我有一个需要连接到远程服务器上的 dBase 文件的 asp.net 网站。远程服务器配置了 ODBC 系统 DSN 连接,但我不知道如何连接到它。

4

1 回答 1

1

服务器上的 ODBC 连接对您没有帮助。ODBC 连接需要在您要连接的机器上设置,而不是在您要连接到的机器上。

为了连接到 DBASE 文件(并将它们视为数据库),您需要

  1. 映射驱动器,以便您可以访问文件的位置..
  2. 连接使用 OleDbConnection。

它还解决了从 .NET 读取 DBase 文件时遇到的问题。如果您经常阅读它们,应用程序将开始抛出“System.Resources.Exceeded”异常。我找到的唯一可靠的解决方案是杀死应用程序并重新启动它,这是在名为 FixMyself 的代码中完成的。(不包括在内,因为它包含敏感数据)。FixMyself 例程基本上启动了第二个 exe,该 exe 杀死了这个 exe,然后重新启动它。

下面的示例代码是从生产代码中复制而来的,应该会给你一个正确的方向。它映射驱动器、连接和读取。

这很丑陋,但它有效。它也只是部分的,因为它调用了这里未包含的几个函数。但同样,它应该足以让你继续前进。

  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
于 2010-08-19T20:09:29.690 回答