English 中文(简体)
如何使用Perl连接到SQL Server?
原标题:How do I connect with Perl to SQL Server?
  • 时间:2011-02-05 07:07:50
  •  标签:
  • perl
  • dbi

我有一个用户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,还是我的程序中缺少什么?

最佳回答

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

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

您可以在connectionstrings.com

问题回答

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

我根据教程创建了TestDBInventory表

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

驱动程序库文件为/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.21

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

 #!/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
";
}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签