use strict; use warnings; use IO::Socket::INET; use DBI; my $db_ip = '10.10.10.10'; my $db_port = 1433; my $db_timeout = 2; my $db_conn_error; my $sock = IO::Socket::INET->new( PeerPort => $db_port, PeerAddr => $db_ip, Proto => 'tcp', Timeout => $db_timeout) or $db_conn_error = "impossible to comunicate with the DB($@)"; if ($sock and $sock->connected){ shutdown($sock, 2); my $connect_string = "driver={SQL Server};Server=10.10.10.10;Database=NAME;UID=USR;PWD=PWD"; my $db_error; my $dbh; if ( $dbh = DBI ->connect ( "DBI:ODBC:host=localhost;$connect_string" )){ my $sth = $dbh->prepare('SELECT * FROM TABLE'); # not the real query.. my $results = $dbh->selectall_arrayref($sth); # OK code here... } else { print "error connecting to the db NAME"; } } else{ print $db_conn_error,"\n"; }