How to connect ms sql database using perl in Windows? - sql-server

I want to connect ms sql database through perl language. The following steps are I made.
I've created a database table in the name "SampleDb"
Configured ODBC and I've set name for sql server driver as "SampleDb"
Now I've tried to connect as follows,
,
my $dbs = "dbi:ODBC:DRIVER={SQL Server};SERVER={SampleDb}";
my $dbh = DBI->connect($dbs, "username", "password");
But Now I got the following error
DBI connect('DRIVER={SQL Server};SERVER={SampleDB}','username',...) failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001) [state was 08001 now 01000]
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000)
How to solve this ? or
How to Connect ms sql using perl through ODBC ?

This question has already been answered (and accepted !!). Just providing detailed steps for connecting MSSQL server from perl running on Linux using ODBC.
Before you get into perl stuff, you need to install and configure odbc environment on the Linux box.
Install below packages:
Fedora:
unixODBC-devel.i686
unixODBC.i686
AND
freetds.i686
freetds-devel.i686
freetds-doc.i686
Ubuntu:
unixodbc
unixodbc-dev
AND
freetds-bin
freetds-common
freetds-dev
tdsodbc
After these packages are installed we MUST be able to use freetds to test authentication network authentication with the MS SQL server.
root#ubuntu:/home# tsql -S <db_host_name> -p 1433 -U perluser -P password
locale is "en_IN"
locale charset is "UTF-8"
1>
If above fails, then check the MSSQL db permission for this user. Also check MSSQL logs. Do not proceed until you run above command successfully. Also, failure of above command has nothing to do with the unixodbc OR freetds configuration.
Configure the odbc.ini and odbcinst.ini and freetds.conf files.
/etc/odbc.ini will contain the DSN information:
root#ubuntu:/home# cat /etc/odbc.ini
[odbc-test]
Description = test
Driver = ms-sql
Servername = ms-sql
Database = <db_name>
UID = perluser
Port = 1433
Please note that, the Driver field refers to /etc/odbcinst.ini context named [ms-sql].
The Servername field refers to the /etc/freetds.conf context that I also named [ms-sql].
Database field is optional but I am using it as I want to connect to a particular Database.
/etc/odbcinst.ini file contains the Driver information:
root#ubuntu:/home# cat /etc/odbcinst.ini
[ms-sql]
Description = TDS Conection
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
UsageCount = 1
FileUsage = 1
The odbcinst.ini file simply directs the odbc.ini file to the appropriate driver.
The Driver and Setup entries for Fedora is /usr/lib/libtdsodbc.so and /usr/lib/libtdsS.so respectively.
Also, for 64-bit Linux box, it would be Driver64 and Setup64 instead of Driver and Setup respectively.
Configure /etc/freetds/freetds.conf ( /etc/freetds.conf for Fedora). It contains TDS information. Here is mine:
root#ubuntu:/home# tail /etc/freetds/freetds.conf
[ms-sql]
host = <db_host_name>
port = 1433
tds version = 7.0
dump file = /var/log/freetds.log
Please note that, above context name [ms-sql] is the value of Servername in odbc.ini.
Test the configuration by connecting the MSSQL server using isql command:
root#ubuntu:/home# isql -v odbc-test perluser password
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
The odbc-test portion of the isql command was defined in the odbc.ini file.
You HAVE to receive above output which says Connected!. If you don’t, then there is some problem in your configuration and go through all the above steps again.
Now,
Install Perl DBI module.
Install DBD::ODBC module .
Then use the ODBC in your perl code as below:
my $dbh = DBI->connect ('dbi:ODBC:odbc-test', 'perluser', 'password');
Hope This Helps.

This is for connecting MSSQL using perl in Windows.
Instead of ODBC, try OLEDB. The following code will help you.
my $host = 'sample\sql';
my $database = 'SampleDB';
my $user = 'sa';
my $auth = 'password';
$dsn = "Provider=sqloledb;Trusted Connection=yes;";
$dsn .= "Server=$host;Database=$database";
my $dbh = DBI->connect("dbi:ADO:$dsn",$user,$auth,{ RaiseError => 1, AutoCommit => 1}) || die "Database connection not made: $DBI::errstr";
Reference How to connect Ms Sql using Perl

Related

Error 20009 (severity 9): Unable to connect: Adaptive Server is unavailable or does not exist OS error 111, "Connection refused"

I tried to connect to MSSQl database using freetds, unixODBC in raspberry pi 4.Below are my settings.
[fred]
host = 109.*.*.* # Remote Sql Server's IP addr
port = 1433 # this is default
tds version = 7.4 # this is by the time i post this
client charset =UTF-8
[test]
Driver = FreeTDS
Description = MSSQL Server
Trace = No
Server = fred # IP or host name of the Sql Server
Port = 1433 # This is default
TDS_Version = 7.4
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup=/usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so
UsageCount=1
I have tested with tsql -H ip -U username -P password but the error still persists. I have also installed pyodbc. I am trying to connect from Pi to MS SQl Server Management studio. Could anyone suggest me to have a successful connection to database? I appreciate all the approaches.
I was able to shrug off this error by using the following call:
bsqldb -S $servername:1433 -D $databasename -U $username -P $password -i $inputfile
As far as documentation states, a qualified server name with the port number allows to bypass the related .conf settings.

Asterisk ODBC show is empty

I'm new to Asterisk and I'm trying to connect asterisk to MS SQL server as I knew that Asterisk + DB will be very powerful. I followed the same steps in Asterisk™: The Definitive Guide (Asterisk book)
http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/installing_configuring_odbc.html#Database_id246915
/etc/odbcinst.ini
[FreeTDS]
Description = ODBC for MSSQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1
Fileusage = 1
Threading = 2
/etc/odbc.ini
[MSSQL]
Description = MSSQL Database
Driver = FreeTDS
Database = asterisk
Server = 192.168.14.193
Username = sa
Password = iti
Trace = no
TDS_Version = 8.0
Port = 1433
when I use this statement
echo "select 1" | isql -v MSSQL
I got
[s1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[s1000][unixODBC][FreeTDS][SQL Server]Login failed for user ''.
[ISQL]ERROR: Could not SQLConnect
but when I used this echo "select 1" | isql -v MSSQL sa iti
it worked
continuing the tutorial
I recompiled Asterisk to feel the changes after installing unixODBC
I modified the res_odbc.conf file
/etc/asterisk/res_odbc.conf
[asterisk]
enabled => yes
dsn => MSSQL
username => sa
password => iti
pooling => no
pre-connect => yes
I also Modified Freetds.conf
/etc/freetds/freetds.conf
[MSSQL]
Server = 192.168.14.193
TDS_Version = 8.0
Port = 1433
when I use odbc show I got this
*CLI> odbc show
ODBC DSN Settings
-----------------
*CLI>
I have already checked the res_odbc, func_odbc while I make menu select
I am using MS SQL Server 2014 installed on my machine and Ubuntu 16 desktop
Please help me solve the issue and Thanks in advance
You have setup connection in res_config_odbc.conf in asterisk directory(seams like you missed that step)
You can test that by
asterisk -rvv
module unload res_config_odbc.so
module load res_config_odbc.so
In odbc.ini file, the username key should be UID and not Username.
See: http://www.freetds.org/userguide/odbcconnattr.htm
Try it then validate with:
echo "select 1" | isql -v MSSQL

ubuntu tsql working and isql not working

I'm trying to connect MSSQL server to ubuntu 14.04 with FreeTDS and unixODBC.
I can connect using tsql with:
~$ tsql -S MSSQL -U <username> -P <password>
This works correctly.
When trying to use isql i get the following
isql -v MSSQL <username> <password>
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[ISQL]ERROR: Could not SQLConnect
Here are my configs.
odbc.ini
[MSSQL]
Driver = FreeTDS
TDS_Version = 8.0
Server = <server>
Instance = dev
Database = <database>
odbcinst.ini
[FreeTDS]
Description = FreeTDS
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
Trace = Yes
TraceFile = /tmp/freetds.log
ForceTrace = Yes
freetds.conf
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
# tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[egServer70]
host = ntmachine.domain.com
port = 1433
tds version = 7.0
[MSSQL]
host = <host>
instance = dev
tds version = 8.0
I think the issue is being caused by the driver, I don't believe i should get an error saying the driver isn't executable. here is the osql output:
osql -S MSSQL -U <username> -P <password>
checking shared odbc libraries linked to isql for default directories...
strings: '': No such file
trying /tmp/sql ... no
trying /tmp/sql ... no
trying /etc ... OK
checking odbc.ini files
reading /home/<user>/.odbc.ini
[MSSQL] found in /home/<user>/.odbc.ini
found this section:
[MSSQL]
Driver = FreeTDS
TDS_Version = 8.0
Server = <server>
Database = <database>
looking for driver for DSN [MSSQL] in /home/<user>/.odbc.ini
found driver line: " Driver = FreeTDS"
driver "FreeTDS" found for [MSSQL] in .odbc.ini
found driver named "FreeTDS"
"FreeTDS" is not an executable file
looking for entry named [FreeTDS] in /etc/odbcinst.ini
found driver line: " Driver = /usr/local/lib/libtdsodbc.so"
found driver /usr/local/lib/libtdsodbc.so for [FreeTDS] in odbcinst.ini
/usr/local/lib/libtdsodbc.so is an executable file
"Server" found, not using freetds.conf
Server is <server_name>
Configuration looks OK. Connection details:
DSN: MSSQL
odbc.ini: /home/<user>/.odbc.ini
Driver: /usr/local/lib/libtdsodbc.so
Server hostname: <host>
Address: <ip_address>
Attempting connection as <username> ...
+ isql MSSQL <username> <password> -v
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[ISQL]ERROR: Could not SQLConnect
Any help is appreciated, Thanks.
I found the answer to my question. I needed to add the instance to the server in the odbc.ini file like so
MSSQL]
Driver = FreeTDS
TDS_Version = 8.0
Server = <server\dev>
Database = <database>
This let me execute isql.

Connecting to MonetDB using ODBC

I have tried using unixODBC (version 2.3.2) and the MonetDB ODBC client (version 11.21.5) on both Mac OS X 10.9 and Debian 8, but cannot connect through ODBC using isql on client machines to a MonetDB server running on Debian 8, although it works fine from the server machine.
When issuing isql -v voc on the client machines I get the following error message:
[08001][unixODBC][MonetDB][ODBC Driver 11.21.5]Client unable to establish connection
[ISQL]ERROR: Could not SQLConnect
Here is my .odbc.ini file:
[voc]
Driver = MonetDB
Description = MonetDB voc test database
Servername = <<left out>>
Port = 50000
UserName = monetdb
Password = monetdb
Database = voc
and my /opt/local/etc/odbcinst.ini file:
[MonetDB]
Description = ODBC Driver for MonetDB SQL Server
Driver = /usr/local/monetdb/lib/libMonetODBC.so
Setup = /usr/local/monetdb/lib/libMonetODBCs.so
FileUsage = 1
Interestingly enough I am able to connect using mclient from client machines and the server machine as well using the following command line:
mclient -d voc -u monetdb -h <<left out>>
The server is configured as follows:
monetdbd get all testing/testDBfarm/
property value
hostname <<left out>>
dbfarm testing/testDBfarm/
status monetdbd[37007] 1.7 (Jul2015) is serving this dbfarm
mserver /usr/bin/mserver5
logfile testing/testDBfarm//merovingian.log
pidfile testing/testDBfarm//merovingian.pid
sockdir /tmp
port 50000
exittimeout 60
forward proxy
discovery true
discoveryttl 600
control no
passphrase <unknown>
mapisock /tmp/.s.monetdb.50000
controlsock /tmp/.s.merovingian.50000
Is there anything I am missing here which is needed to let clients connect to a MonetDB server through ODBC?
The debug log of isql (running ODBCDEBUG=/tmp/odbclog isql test) revealed the problem:
The MonetDB ODBC driver has ignored the Servername parameter in my config file.
.odbc.ini:
Servername = LEFT OUT
Log:
SQLConnect: DSN=voc UID=monetdb PWD=monetdb host=localhost port=50000 database=voc.
It is quite unfortunate, that most ODBC drivers use a different name for specifying the server's hostname or IP-address: Servername, Host, or even Server (See: http://www.unixodbc.org/odbcinst.html)
Thus, for MonetDB's ODBC driver we need to use Host to specify the hostname, instead of Servername or Server, as for Postgres or MySQL, e.g.

freeTDS not using its config

I've decided to use FreeTDS driver and unixODBC to manage the PDO connection between my LAMP-based app with a remote MsSQL database.
unfortunately it appears that the driver doesn't read the freetds.conf file, nor the environmental variables set either directly via server's CLI or specified in php file by putenv() function.
now some data:
as I ping the server - no packets are lost.
as I telnet the server on 1433 port - the connection is established
as I use the command
TDSVER=7.0 tsql -H >IP< -p 1433 -U username
I am prompted to enter password and connection is established.
without the TDSVER preceding the command - the connection fails with such a message:
Error 20017 (severity 9):
Unexpected EOF from the server
OS error 115, "Operation now in progress"
Error 20002 (severity 9):
Adaptive Server connection failed
There was a problem connecting to the server
the tsql -C commands echos such an output:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /usr/local/etc
MS db-lib source compatibility: yes
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 5.0
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: no
freetds.conf in the location given above has this entry:
[MSSQL]
host = >IP<
port = 1433
tds version = 7.0
the ISQL also fails:
isql -v MSSQL
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
[ISQL]ERROR: Could not SQLConnect
my odbc.ini :
[MSSQL]
Description = MS SQL Server
Driver = FreeTDS
TDS_Version = 7.0
Server = >IP<
UID = username
PWD = password
ReadOnly = No
Port = 1433
I suppose the solution is really simple, but i'm just too stupid to find it...
I spent a long time today debugging a similar problem. I had set "TDS version" in freetds.conf but it was not being used in my ODBC connection. After reading the freetds source code (connectparams.c:odbc_parse_connect_string) I discovered that:
If your connection string uses "SERVER=" then both freetds.conf and odbc.ini are ignored
If your connection string uses "SERVERNAME=" then the settings in the appropriate freetds.conf server are used
If your connection string uses "DSN=" then the settings in the appropriate odbc.ini DSN are used
odbcinst.ini is a red herring. FreeTDS never checks that for settings.
The settings you specify in the connection string are always respected. It also always respects the environment variables like TDSVER.
My gut instinct is you need to change your tds version = 7.0 to tds version = 8.0 in your freetds.conf and odbc.ini files AND you need something in your odbcinst.ini file. Here's what I've got working on an Ubuntu 12.04 server talking to a remote MSSQL server:
freetds.conf
# Define a connection to the MSSQL server.
[mssql]
host = myserver
port = 1433
tds version = 8.0
odbc.ini
# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description = MSSQL Server
Driver = freetds
Database = MyDB
ServerName = myserver
TDS_Version = 8.0
odbcinst.ini
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
I experienced this exact same problem, but my config was already correctly set up. The problem was that the TDS versions that freetds.conf recognizes has changed in newer versions, but apparently the old versions still work in the TDSVER environment variable. Once I set the version in the config files to 7.1 rather than 8.0 everything started working.
The initial TDSVER=7.0 issue was resolved by adding the following at the end of my odbc.ini file:
[Default]
Driver=/usr/local/lib/libtdsodbc.so
Instead of connecting to an IP, try to connect to the name of the configuration section? For example:
isql -v MSSQL

Resources