如何创建到 SAS 的 ODBC 连接?
我正在编写一个需要访问 SAS 数据的程序。我已经下载了 SAS 的 ODBC 驱动程序并安装了它们,但我需要能够以编程方式动态创建 ODBC 连接。以下代码(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')
但它会弹出 SAS ODBC 配置对话框,设置数据源名称,然后等待用户输入信息并关闭对话框。我怎样才能避免这种情况?
I'm writing a program that needs to access SAS data. 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. The following code (in Python) seems like it should work:
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. How can I avoid that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了通过 ODBC 访问 SAS 数据,您需要连接到某种正在运行的 SAS 会话;您无法使用 SAS ODBC 驱动程序直接访问 SAS 数据表文件。
请参阅 SAS ODBC 驱动程序指南 ,“我需要什么软件?”部分。
您的问题并未表明您正在尝试通过正在运行的 SAS 产品访问 SAS 数据。 SAS ODBC 驱动程序指南应告诉您如何根据要通过其进行连接的 SAS 产品设置连接。
In order to get ODBC access to SAS data, you need to connect to a running SAS session of some kind; you can't access SAS data table files directly with the SAS ODBC drivers.
See the SAS ODBC drivers guide, section "What Software Do I Need?".
Your question doesn't state that you are trying to access SAS data through a running SAS product. 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.
我知道这个问题很古老,但它可能经常出现,所以我会分享我所知道的答案。实现您想要做的事情的最简单方法是创建所谓的“无 DSN 连接”。
简而言之,您创建一个在打开连接时使用的连接字符串,该字符串标识驱动程序并包含您通常在为该驱动程序创建 DSN 时填写的所有参数。
这项技术已经存在很长时间了——我在 2001 年使用它,而且它的历史更早。
以下是一系列用于通过 ODBC 访问 SAS 数据的无 DSN 连接字符串示例:
使用服务器 ID
Provider=sas.ShareProvider;数据源=shr1;
SAS/SHARE
使用服务器 ID 和节点(网络名称)
提供商=sas.ShareProvider;数据源=shr1;位置=lambchop.unx.sas.com;
SAS/共享
指定用户和密码
Provider=sas.ShareProvider;数据源=shr1;位置=lambchop.unx.sas.com;
用户 ID=myUsername;密码=myPassword;
SAS/SHARE
服务器需要密码
Provider=sas.ShareProvider;数据源=shr1;位置=lambchop.unx.sas.com;
用户 ID=myUsername;密码=myPassword;
SAS 服务器访问密码=myServerPassword;
这些来自:
https://www.connectionstrings.com/sas-share/
SAS 支持页面对此进行了讨论更多--我在这里找到了它:
http://docslide.us/documents/opening-an-ado-connection -object.html
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."
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.
This technique has been around for a very long time-- I was using it in 2001, and it's older than that.
Here is a series of sample DSN-Less connection strings for accessing SAS data via ODBC:
Using 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
Specifying user and password
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;
These are from:
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 知之甚少,但用于在 ODBC 中动态连接到数据库的函数是 SQLDriverConnect.您不需要(或想要)添加 DSN,您只需要安装驱动程序。
I know zilch about SAS, but the function used to connect to a database on the fly in ODBC is SQLDriverConnect. You don't need (or want) to add a DSN, you just need the drivers installed.