connection string to MS sql server with DBI

Is there any simple way to connect to a sql server running on windows from a linux m/c using the perl DBI module??? Do i need to download obbc drivers? Any help appreciated..........Also FYI,  I am using windows authentication to login to the sql server (in which I created the database). So what user name and password should I use in my connection string? I want to insert values from a csv (in the linux m/c) into the database tables in the sql server.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

ronan_40060Connect With a Mentor Commented:
use DBI;
use DBD::ODBC;
$dbusername         = "subirc";  
$dbpassword         = "subir123";  
$server=" xxxxxxxxx";
$dbh = DBI-> connect( "DBI:ODBC:driver={SQL Server};Server=$server;Database=$database;UID=$dbusername;PWD=$dbpassword")  || die &DBerror;

replace the $server name with ur server
Check this out:

I currently use it to connect to MS SQl Server from a Linux box. I use PHP but you should have no problem using Perl.
For the authentication you would have to give valid login information. I use the sa account but my application sits behind a firewall so I am not too worried about security.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

hello subir

U can check the following link about connecting to MS SQL server using DBI
good luck
subircAuthor Commented:
Ronan I did check that....looks like things like things r lil bit different there than mine. My database doesn't have a username, password .......and i login to the sql server with windows authentication. So does that change my connection string?

Hi subirc,

As Sasho already mentioned, you can use freetds for the odbc connection. Recent versions also support windows authentication, so make sure you read the documentation. As far as I can remember, you supply the windows account info in the freetds configuration file, and if that works, you should be able to connect without a user and password, i.e. $dbh = DBI->connect('dbi:ODBC:yourdsn','','');

An alternative would be to use dbiproxy. I have been meaning to look into this (I'm running Debian Woody, and the freetds version for that is really old, and doesn't support windows auth, which IMHO is an absolute requirement), but haven't had the time yet.
The basic idea is that you connect from the linux box using the dbiproxy client, i.e., something like $dbh = DBI->connect("dbi:Proxy:hostname=$host;port=$port;dsn=$db", $user, $passwd);
You'd then have to run a dbiproxy server on another host (quite possibly the database server) that does the odbc for you.
It should have the advantage of being able to do all the odbc stuff you need, without having to install it on the linux box.

subircAuthor Commented:
Ok time to come back to you experts!!!

I have my DBI script as follows

my $sql = "select * from schemadzu";
my $dsn = 'DBI:ODBC:LocalServer';
my $dbh = DBI->connect($dsn, '', '') || die &DBerror; #since I m using windows authentication, I shouldn't have a ID & passwd
my $sth = $dbh->prepare($sql) || die;

Also freeTDS and unixODBC is installed.

My odbc.ini file (under unixodbc)  looks like this

;  odbc.ini
[ODBC Data Sources]
dbprogrammer=freetds driver mssql 2000



I still can't get a connection to the database. why?   The ERROR I am getting is listed below:

DBI connect('LocalServer','',...) failed: [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed (SQL-IM004)(DBD: db_login/SQLConnect err=-1) at line 8

Note: my data source name is "LocalServer" and my sqlserver name is "DBPROGRAMMER".

subircAuthor Commented:
ok i added a user name and a password to my database (thinking that windows authentication was the problem). The new conn string looks like this...........

my $dbh = DBI->connect($dsn, 'subirc', 'subir123') || die &DBerror;

Still getting same error

DBI connect('LocalServer','',...) failed: [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed (SQL-IM004)(DBD: db_login/SQLConnect err=-1) at line 8
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.