Solved

connection string to MS sql server with DBI

Posted on 2004-08-11
8
1,739 Views
Last Modified: 2008-01-09
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.

thanks!!
Subir.
0
Comment
Question by:subirc
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:Sasho
ID: 11778070
Check this out: http://www.freetds.org/

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.
0
 
LVL 3

Expert Comment

by:Sasho
ID: 11778078
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.
0
 
LVL 9

Expert Comment

by:ronan_40060
ID: 11778527
hello subir

U can check the following link about connecting to MS SQL server using DBI

http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21057322.html
good luck
ronan
0
 

Author Comment

by:subirc
ID: 11778624
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?

Subir.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Expert Comment

by:kandura
ID: 11779500
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.

HTH,
Kandura
0
 

Author Comment

by:subirc
ID: 11815903
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;
$sth->execute();
....
.....
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

[ODBC]
TraceFile=/tmp/odbc.trace
Trace=1

[dbprogrammer]
Driver=/usr/local/opt/freetds/lib/libtdsodbc.so
Server=dbprogrammer.bowdoin.edu
Database=DB201
Port=1433
TDS_Version=8.0

[DEFAULT]
Driver=/usr/local/opt/freetds/lib/libtds.so
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 connect_to_sqlserver.pl line 8
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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


0
 

Author Comment

by:subirc
ID: 11816074
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 connect_to_sqlserver.pl line 8
0
 
LVL 9

Accepted Solution

by:
ronan_40060 earned 100 total points
ID: 11823926
   try
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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now