2

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

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
Alla Sasikanth
  • 541
  • 3
  • 7
  • 22
  • 3
    Please don't post text as images. It is very difficult to read, and impossible to copy and paste. You should also remember our many blind subscribers. – Borodin Sep 13 '16 at 09:02
  • 4
    @Borodin: I'm not entirely blind (85%), but I couldn't agree with you more :) – DerpyNerd Sep 13 '16 at 09:06

1 Answers1

2

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',
    }
);
Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133