我有用户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,还是我的程序中缺少任何东西?
答案 0 :(得分:17)
连接字符串中'dbi:ODBC:'
之后的所有内容都会传递给ODBC驱动程序。对于MSSQL,请尝试以下连接字符串:
DBI->connect("dbi:ODBC:Driver={SQL Server};Server=192.168.3.137;UID=$user;PWD=$password")
上找到更多替代方案
答案 1 :(得分:1)
我在Ubuntu(20.04)上,并按照说明在Docker容器中安装SLQ Server: https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash
我按照本教程创建了TestDB
和表Inventory
。
我在下面安装了ODBC驱动程序 https://docs.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";
}