Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Answer: How do I connect to Microsoft SQL server 6.5?

by patgas (Friar)
on Jan 23, 2002 at 20:43 UTC ( #140929=categorized answer: print w/replies, xml ) Need Help??

Q&A > database programming > How do I connect to Microsoft SQL server 6.5? - Answer contributed by patgas

It took me weeks to figure out the actual code on how to connect to SQL Server without setting up a DSN on the machine. Hopefully I can spare someone the same pain.

This should give you the basics:

use DBI; use DBD::ODBC; my ( $data_source, $database, $user_id, $password ) = qw( server datab +ase user password ); my $conn_string = "driver={SQL Server};Server=$data_source;Database=$d +atabase;UID=$user_id;PWD=$password"; my $dbh = DBI->connect( "DBI:ODBC:$conn_string" ) or die $DBI::errstr; my $sql = "SELECT * FROM tbl_Foo (NOLOCK)"; my $sth = $dbh->prepare( $sql ); $sth->execute; while ( my $result = $sth->fetchrow_hashref ) { # Your fields can be accessed through the $result hashref, for exa +mple: print $result->{First_Name}; } $dbh->disconnect;

Log In?

What's my password?
Create A New User
[choroba]: Hi Corion!
[Discipulus]: hello folks!
[Corion]: Hi choroba, Discipulus!
[Corion]: choroba: It's currently evening for you, isn't it? Or are you up in the middle of the night?

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-02-23 08:54 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (342 votes). Check out past polls.