如何使用 Perl 连接到 SQL Server?
我有用户 ID、密码、数据库名称和数据源详细信息。我想用 Perl 连接到 MSSQL 服务器。我刚刚使用了以下代码片段,但出现错误。
#!/usr/bin/perl -w
use strict;
use DBI;
my $data_source = q/dbi:ODBC:192.168.3.137/;
my $user = q/bharani/;
my $password = q/123456/;
# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
or die "Can't connect to $data_source: $DBI::errstr";
我的错误是:
DBI connect('192.168.3.137','bharani',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at my sqlconnect.pl line 14
Can't connect to dbi:ODBC:192.168.3.137: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at mysqlconnect.pl line 14.
SQL 服务器在另一个系统上运行,我只是尝试连接上述详细信息。请告诉我,我应该在系统中添加 DSN,还是我的程序中缺少任何内容?
I have a user id, password, database name and datasource details. I want to connect with Perl to a MSSQL server. I just used the following snippet, but I am getting an error.
#!/usr/bin/perl -w
use strict;
use DBI;
my $data_source = q/dbi:ODBC:192.168.3.137/;
my $user = q/bharani/;
my $password = q/123456/;
# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
or die "Can't connect to $data_source: $DBI::errstr";
My error is:
DBI connect('192.168.3.137','bharani',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at my sqlconnect.pl line 14
Can't connect to dbi:ODBC:192.168.3.137: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at mysqlconnect.pl line 14.
The SQL server runs on another system, I am just trying to connect with above details. Please tell me, should I crease DSN in my system, or is anything missing in my program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
连接字符串中
'dbi:ODBC:'
后面的所有内容都会传递到 ODBC 驱动程序。对于 MSSQL,请尝试此连接字符串:您可以在 connectionstrings.com 上找到更多替代方案
Everything following
'dbi:ODBC:'
in your connection string is passed to the ODBC driver. For MSSQL, try this connection string:You can find some more alternatives on connectionstrings.com
我使用的是 Ubuntu (20.04),并按照说明在 docker 容器中安装 SLQ Server:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash
我创建了
TestDB
和表Inventory
按照教程。我安装了以下 ODBC 驱动程序
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server? view=sql-server-ver15
驱动程序库文件为
/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1
这样,以下脚本成功连接到数据库并返回表的内容:
I am on Ubuntu (20.04) and followed the instructions to install SLQ Server in a docker container:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash
I created the
TestDB
and the tableInventory
as per the tutorial.I installed the ODBC driver following
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
The driver library file was
/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1
With that, the following script successfully connects to the DB and returns the content of the table: