将 Rails 应用程序从 Windows 连接到 SQL Server 2005
我(遗憾地)必须在Windows XP 上部署一个rails 应用程序,该应用程序必须连接到Microsoft SQL Server 2005。
在网上冲浪有很多关于从 Linux 连接到 SQL Server 的内容,但找不到如何从 Windows 进行连接。
基本上我遵循了以下步骤:
- 安装dbi gem
- 安装activerecord-sql-server-adapter gem
我的database.yml现在看起来像这样:
development:
adapter: sqlserver
mode: odbc
dsn: test_dj
host: HOSTNAME\SQLEXPRESS
database: test_dj
username: guest
password: guest
但是我无法连接它。当我运行rake db:migrate时,我发现
IM002 (0) [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
我不是Windows用户,因此无法很好地理解dsn元素等的含义。 有人知道如何解决这个问题吗?
提前致谢
现在有了Alexander提示,我已经将我的database.yml修改为:
development:
adapter: sqlserver
mode: odbc
dsn: Provider=SQLOLEDB;Data Source=SCADA\SQLEXPRESS;UID=guest;PWD=guest;Initial Catalog=test_dj;Application Name=test
但是现在rake db:migrate返回了我:
S1090 (0) [Microsoft][ODBC Driver Manager] Invalid string or buffer length
我错过了什么吗?
I (sadly) have to deploy a rails application on Windows XP which has to connect to Microsoft SQL Server 2005.
Surfing in the web there are a lot of hits for connect from Linux to SQL Server, but cannot find out how to do it from Windows.
Basically I followed these steps:
- Install dbi gem
- Install activerecord-sql-server-adapter gem
My database.yml now looks like this:
development:
adapter: sqlserver
mode: odbc
dsn: test_dj
host: HOSTNAME\SQLEXPRESS
database: test_dj
username: guest
password: guest
But I'm unable to connect it. When I run rake db:migrate I get
IM002 (0) [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I'm not a Windows user, so cannot understand really well the meaning of dsn element or so.
Does someone have an idea how to solve this?
Thanks in advance
With Alexander tips now I've modified my database.yml to:
development:
adapter: sqlserver
mode: odbc
dsn: Provider=SQLOLEDB;Data Source=SCADA\SQLEXPRESS;UID=guest;PWD=guest;Initial Catalog=test_dj;Application Name=test
But now rake db:migrate returns me:
S1090 (0) [Microsoft][ODBC Driver Manager] Invalid string or buffer length
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个示例 DSN,它使用 Windows 用户帐户连接到数据库(最好是在具有域登录的公司网络时)
Provider=SQLOLEDB;Data Source=MyServer\MyInstance;Integrated Security=SSPI;Initial Catalog=MyDatabase;Application Name=将在跟踪中显示的我的应用程序名称
因此,这使用 SQL Server 的 OLEDB 提供程序。 SQLNCLI也可以用,ODBC没试过。实际上这个 DSN 还没有经过充分测试(必须等待我的管理员给我必要的权限),但它是从使用 SQLNCLI 作为提供程序的工作脚本复制的。数据源是服务器,如果它有命名实例,则必须指定它,因此它要么只是
Server
,要么是Server\Instance
。Integrated Security=SSPI
告诉它您要使用 Windows 身份验证。否则,您可以使用UID=MyUser;PWD=MyPassword
指定要使用的用户和密码。 UID、用户、用户名、密码 - 我认为所有这些参数都有效。互联网上有一个很棒的网站提供各种 DSN 示例,但到处都找不到。如果我找到了,我会告诉你的。
如果 Rails 中没有任何提供程序,请检查 Rails 是否支持 Windows 组件对象模型 (COM)。如果是这样,您甚至可以初始化 ADODB.Connection COM 类,并在其上使用 ADO。
啊,这是我正在谈论的那个网站: http://www.connectionstrings.com/
this is a sample DSN, that connects to the database using the Windows user account (best when corporate network with domain login)
Provider=SQLOLEDB;Data Source=MyServer\MyInstance;Integrated Security=SSPI;Initial Catalog=MyDatabase;Application Name=My Application Name that will show up in the trace
So this uses the OLEDB provider for SQL Server. SQLNCLI can also be used, haven't tried it with ODBC. Actually this DSN isn't quite tested (have to wait for my admin to give me the necessary rights), but it was copied from a working script, that used SQLNCLI as the provider. The Data source is the server, and if it has a named instance, it has to be specified, so it's either just
Server
orServer\Instance
. TheIntegrated Security=SSPI
tells it you want to use Windows Authentication. Otherwise you specify the user and password to use usingUID=MyUser;PWD=MyPassword
. UID, User, Username, Password - I think all of these parameters work.There is a great site over the internet that provides all kinds of DSN samples, just can't find it anywhere. If I find it, I will let you know.
If you haven't got any provider in Rails, check out if Rails supports Windows Component Object Model (COM). And if it does, you can even initialize the ADODB.Connection COM class, work with ADO thereon.
Ah, here is that site I was talking about: http://www.connectionstrings.com/