简体   繁体   English

如何创建与SAS的ODBC连接?

[英]How can I create an ODBC connection to SAS?

I'm writing a program that needs to access SAS data. 我正在编写一个需要访问SAS数据的程序。 I've downloaded the ODBC drivers for SAS and installed them, but I need to be able to create ODBC connections on the fly, programmatically. 我已经下载了SAS的ODBC驱动程序并安装了它们,但我需要能够以编程方式动态创建ODBC连接。 The following code (in Python) seems like it should work: 以下代码(在Python中)似乎应该工作:

import ctypes

ODBC_ADD_DSN = 1        

def add_dsn(name, driver, **kw):
    nul, attrib = chr(0), []
    kw['DSN'] = name
    for attr, val in kw.iteritems():
        attrib.append('%s=%s' % (attr, val))

    return ctypes.windll.ODBCCP32.SQLConfigDataSource(0, ODBC_ADD_DSN, driver, nul.join(attrib)) == 1

print add_dsn('SAS Test', 'SAS', description = 'Testing SAS')

But it pops up the SAS ODBC configuration dialog, sets the datasource name, and waits for the user to enter the information and dismiss the dialog. 但它会弹出SAS ODBC配置对话框,设置数据源名称,并等待用户输入信息并关闭对话框。 How can I avoid that? 我怎么能避免这种情况?

In order to get ODBC access to SAS data, you need to connect to a running SAS session of some kind; 为了获得对SAS数据的ODBC访问,您需要连接到某种运行的SAS会话; you can't access SAS data table files directly with the SAS ODBC drivers. 您无法使用SAS ODBC驱动程序直接访问SAS数据表文件。

See the SAS ODBC drivers guide , section "What Software Do I Need?". 请参阅SAS ODBC驱动程序指南 ,“我需要什么软件?”部分。

Your question doesn't state that you are trying to access SAS data through a running SAS product. 您的问题并未声明您正在尝试通过正在运行的SAS产品访问SAS数据。 The SAS ODBC drivers guide should tell you how to set up the connection based on the SAS product you will make the connection through. SAS ODBC驱动程序指南应该告诉您如何根据要进行连接的SAS产品设置连接。

I know this question is ancient but it probably comes up often enough that I will share the answer I know. 我知道这个问题很古老,但它可能经常出现,我将分享我所知道的答案。 The easiest way to achieve what you are trying to do is create what's called a "DSN-Less Connection." 实现您要做的事情的最简单方法是创建所谓的“DSN-Less Connection”。

Briefly, you create a connect string that you use when opening a connection, that identifies the driver and includes all the parameters you'd normally fill in in creating a DSN for that driver. 简而言之,您创建了一个在打开连接时使用的连接字符串,用于标识驱动程序并包含您在为该驱动程序创建DSN时通常填写的所有参数。

This technique has been around for a very long time-- I was using it in 2001, and it's older than that. 这项技术已经存在了很长时间 - 我在2001年使用它,它比它更老。

Here is a series of sample DSN-Less connection strings for accessing SAS data via ODBC: 以下是一系列用于通过ODBC访问SAS数据的DSN-Less连接字符串示例:

Using Server ID Provider=sas.ShareProvider;Data Source=shr1; 使用Server ID Provider=sas.ShareProvider;Data Source=shr1;

SAS/SHARE Using Server ID and node (network name) Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; SAS / SHARE使用服务器ID和节点(网络名称)Provider = sas.ShareProvider; Data Source = shr1; Location = lambchop.unx.sas.com;

SAS/SHARE Specifying user and password Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; User ID=myUsername;Password=myPassword; SAS / SHARE指定用户和密码Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; User ID=myUsername;Password=myPassword; Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; User ID=myUsername;Password=myPassword;

SAS/SHARE Server requires a password Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; User ID=myUsername;Password=myPassword; SAS Server Access Password=myServerPassword; SAS / SHARE服务器需要密码Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; User ID=myUsername;Password=myPassword; SAS Server Access Password=myServerPassword; Provider=sas.ShareProvider;Data Source=shr1;Location=lambchop.unx.sas.com; User ID=myUsername;Password=myPassword; SAS Server Access Password=myServerPassword;

These are from: https://www.connectionstrings.com/sas-share/ 这些来自: https//www.connectionstrings.com/sas-share/

A SAS support page discusses this more-- I found it here: http://docslide.us/documents/opening-an-ado-connection-object.html SAS支持页面更多地讨论了这个问题 - 我在这里找到了它: http ://docslide.us/documents/opening-an-ado-connection-object.html

I know zilch about SAS, but the function used to connect to a database on the fly in ODBC is SQLDriverConnect . 我知道zilch关于SAS,但用于在ODBC中动态连接数据库的函数是SQLDriverConnect You don't need (or want) to add a DSN, you just need the drivers installed. 您不需要(或想要)添加DSN,只需安装驱动程序即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 SQLAlchemy 中,我可以从现有的 ODBC 连接创建引擎吗? - In SQLAlchemy, can I create an Engine from an existing ODBC connection? 我是否需要向Azure Databricks添加ODBC连接? 如果是这样,怎么办? - Do I need to add an ODBC Connection to Azure Databricks? If so, how? 给定数据库的 ODBC 连接字符串,如何修改我的 settings.py 以便我的 Django 应用程序连接到它? - Given an ODBC connection string for a database, how can I modify my settings.py such that my Django app will connect to it? 如何在 2 台 PC 之间用 Python 创建 TCP 连接 - How can I create a TCP connection in Python between 2 PCs 什么是ODBC?如何利用它(Linux)? - What is ODBC and how can I take advantage of it (Linux)? 如何使用 pyodbc 访问 ODBC 中表的视图实例 - How can i access Views instance of Tables in ODBC using pyodbc 如何强制SAS等待命令完全执行? - How can I force SAS to wait for a command to fully execute? 如何在 ZA7F5F35426B927411173DZ 中生成 Azure blob SAS URL? - How can I generate an Azure blob SAS URL in Python? 如何使用python 3创建套接字连接? - How do I create a socket connection with python 3? 如何在 Python 中创建 websocket 连接? - How do I create a websocket connection in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM