连接到远程服务器上的 dBase 文件

发布于 2024-09-14 19:04:04 字数 81 浏览 1 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐偏执 2024-09-21 19:04:04

服务器上的 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文