Alla Sasikanth
Alla Sasikanth

Reputation: 563

Connect to SQL Server using DBI

I am trying to connect to MS SQL server using Perl Script. Here is the Code.

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

use DBI;

#my $dbfile = "sample.db";

my $dsn      = "dbi:ODBC:SQLServer:dpnsql";
my $user     = "xxx";
my $password = "******";
my $dbh = DBI->connect($dsn, $user, $password, 
{
   PrintError       => 0,
   RaiseError       => 1,
   AutoCommit       => 1,
   FetchHashKeyName => 'NAME_lc',
}
);


$dbh->disconnect;

But I am getting the error as below enter image description here

Please help me in this issue. Any new code is also appreciated.

TIA

Upvotes: 2

Views: 1392

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21676

The error says that Data source name not found.

That means your dsn is incorrect.

If you're working with an x64 server, keep in mind that there are different ODBC settings for x86 and x64 applications. [See: https://stackoverflow.com/a/5034297/257635]

Try the below syntax with correct DSN.

my $dbh = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=<IP>;UID=$user;PWD=$password",
    {
       PrintError       => 0,
       RaiseError       => 1,
       AutoCommit       => 1,
       FetchHashKeyName => 'NAME_lc',
    }
);

Upvotes: 2

Related Questions