如何使用 Perl 连接到 SQL Server?

发布于 2024-10-16 08:52:17 字数 891 浏览 2 评论 0原文

我有用户 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-10-23 08:52:17

连接字符串中 'dbi:ODBC:' 后面的所有内容都会传递到 ODBC 驱动程序。对于 MSSQL,请尝试此连接字符串:

DBI->connect("dbi:ODBC:Driver={SQL Server};Server=192.168.3.137;UID=$user;PWD=$password")

您可以在 connectionstrings.com 上找到更多替代方案

Everything following 'dbi:ODBC:' in your connection string is passed to the ODBC driver. For MSSQL, try this connection string:

DBI->connect("dbi:ODBC:Driver={SQL Server};Server=192.168.3.137;UID=$user;PWD=$password")

You can find some more alternatives on connectionstrings.com

故事与诗 2024-10-23 08:52:17

我使用的是 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

这样,以下脚本成功连接到数据库并返回表的内容:

 #!/usr/bin/perl
use strict;
use warnings;

use DBI;
my $user = 'SA';
my $password = '<YourNewStrong@Passw0rd>';

my $dbh = DBI->connect("dbi:ODBC:Driver={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1};Server=localhost;Database=TestDB;UID=$user;PWD=$password");

my $sth = $dbh->prepare("SELECT * FROM Inventory");
$sth->execute();

while ( my @row = $sth->fetchrow_array ) {
      print "@row\n";
}

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 table Inventory 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:

 #!/usr/bin/perl
use strict;
use warnings;

use DBI;
my $user = 'SA';
my $password = '<YourNewStrong@Passw0rd>';

my $dbh = DBI->connect("dbi:ODBC:Driver={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1};Server=localhost;Database=TestDB;UID=$user;PWD=$password");

my $sth = $dbh->prepare("SELECT * FROM Inventory");
$sth->execute();

while ( my @row = $sth->fetchrow_array ) {
      print "@row\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文