【问题标题】:Creating a Microsoft Access database connection to a REST-based API创建与基于 REST 的 API 的 Microsoft Access 数据库连接
【发布时间】:2015-09-24 18:16:19
【问题描述】:

我正在尝试在 Microsoft Access 2013 中创建指向由基于 REST 的 API (this API, to be specific) 提供的数据的实时链接。最终目标是让数据在查询中可用,就好像它是本地数据库一样。

如何做到这一点?具体来说,我正在努力解决如何让 Access 根据请求调用 API。我认为获得类似结果的唯一方法是编写一个脚本,通过 API 提取整个数据库并将其转换为 Access 可读格式,然后以设定的时间间隔运行该脚本。但我真的很想找到一个实时工作的解决方案,即使它比本地缓存数据库慢一点。

【问题讨论】:

    标签: api rest ms-access vba database-connection


    【解决方案1】:

    由于对 RESTful Web 服务的调用实际上只是一种特定类型的 HTTP 请求,您至少可以使用 Microsoft XML 库向 Web 服务发送 HTTP 请求并解析它返回的任何内容。例如,当我运行以下 VBA 代码时

    ' VBA project Reference required:
    ' Microsoft XML, v3.0
    
    Dim httpReq As New MSXML2.ServerXMLHTTP
    httpReq.Open "GET", "http://whois.arin.net/rest/poc/KOSTE-ARIN", False
    httpReq.send
    Dim response As String
    response = httpReq.responseText
    Debug.Print response
    

    字符串变量response 包含对我的请求的 XML 响应。它看起来像这样(在重新格式化以提高可读性之后):

    <?xml version='1.0'?>
    <?xml-stylesheet type='text/xsl' href='http://whois.arin.net/xsl/website.xsl' ?>
    <poc xmlns="http://www.arin.net/whoisrws/core/v1" xmlns:ns2="http://www.arin.net/whoisrws/rdns/v1"
    xmlns:ns3="http://www.arin.net/whoisrws/netref/v2" termsOfUse="https://www.arin.net/whois_tou.html"
    inaccuracyReportUrl="http://www.arin.net/public/whoisinaccuracy/index.xhtml">
      <registrationDate>2009-10-02T11:54:45-04:00</registrationDate>
      <ref>http://whois.arin.net/rest/poc/KOSTE-ARIN</ref>
      <city>Chantilly</city>
      <companyName>ARIN</companyName>
      <iso3166-1>
        <code2>US</code2>
        <code3>USA</code3>
        <name>UNITED STATES</name>
        <e164>1</e164>
      </iso3166-1>
      <firstName>Mark</firstName>
      <handle>KOSTE-ARIN</handle>
      <lastName>Kosters</lastName>
      <emails>
        <email>markk@kosters.net</email>
        <email>markk@bjmk.com</email>
      </emails>
      <resources termsOfUse="https://www.arin.net/whois_tou.html"
      inaccuracyReportUrl="http://www.arin.net/public/whoisinaccuracy/index.xhtml">
        <limitExceeded limit="256">false</limitExceeded>
      </resources>
      <phones>
        <phone>
          <number>+ 1-703-227-9870</number>
          <type>
            <description>Office</description>
            <code>O</code>
          </type>
        </phone>
      </phones>
      <postalCode>20151</postalCode>
      <comment>
        <line number="0">I&#39;m really MAK21-ARIN</line>
      </comment>
      <iso3166-2>VA</iso3166-2>
      <streetAddress>
        <line number="0">3635 Concorde Parkway</line>
      </streetAddress>
      <updateDate>2015-05-26T11:36:55-04:00</updateDate>
    </poc>
    

    您的网络服务返回的内容可能看起来有些不同。或者,如上述 ARIN whois RWS 的情况,您可能有多种数据格式可供选择; XML 只是默认设置。我本可以使用

    请求纯文本响应
    httpReq.Open "GET", "http://whois.arin.net/rest/poc/KOSTE-ARIN.txt", False
    

    在这种情况下response 将包含

    #
    # ARIN WHOIS data and services are subject to the Terms of Use
    # available at: https://www.arin.net/whois_tou.html
    #
    
    
    Name:           Kosters, Mark 
    Handle:         KOSTE-ARIN
    Company:        ARIN
    Address:        3635 Concorde Parkway
    City:           Chantilly
    StateProv:      VA
    PostalCode:     20151
    Country:        US
    RegDate:        2009-10-02
    Updated:        2015-05-26
    Comment:        I'm really MAK21-ARIN
    Phone:          +1-703-227-9870 (Office)
    Email:          markk@bjmk.com
    Email:          markk@kosters.net
    Ref:            http://whois.arin.net/rest/poc/KOSTE-ARIN
    #
    # ARIN WHOIS data and services are subject to the Terms of Use
    # available at: https://www.arin.net/whois_tou.html
    #
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      相关资源
      最近更新 更多