http://www.perlmonks.org?node_id=178632


in reply to connecting Perl with MS SQL Server on NT using ODBC

peacemaker1820, I had to include the IP address, Port number, and the domain\server in order to get the MS SQL to work.

Check to make sure that you have current modules as well...
PPM>install DBI PPM>install DBD-ODBC

Here is some sample code to show you what works for my environment (W2K) but it should be similar (if not identical.)

#!/usr/bin/perl -w use strict; use DBI; print "Content-type: text/html\n\n"; # Set up variables for the connection my $server_name = 'domain\server'; my $server_ip = '1.2.3.4:port'; my $database_name = 'Tim'; my $database_user = 'Taylor'; my $database_pass = 'toolman'; my $DSN = 'driver={SQL Server};server=$server_name;tcpip=$server_ip;da +tabase=$database_name;uid=$database_user;pwd=$database_pass;'; my $dbh = DBI->connect("dbi:ODBC:$DSN") || die "Couldn't open database +: $DBI::errstr\n"; # Prepare the SQL query for execution my $SQL1 = $dbh->prepare(<<End_SQL) || die "Couldn't prepare statement +: $DBI::errstr\n"; select * FROM Test_Table End_SQL print '<table border="0" width="100%" cellpadding="0" cellspacing="0" +bgcolor="#ffffff" summary=""><tr><td align="center">'; print '<table border="1" cellpadding="5" cellspacing="0" bgcolor="#fff +fff" summary="">'; # Execute the query $SQL1->execute() || die "Couldn't execute statement: $DBI::errstr\n"; # Fetch each row and print it while ( my ($field1,$field2,$field3,$field4,$field5) = $SQL1->fetchrow +_array() ) { print "<tr><td>$field1</td><td>$field2</td><td>$field3</td><td>$f +ield4</td><td>$field5</td></tr>"; } print "</table></td></tr></table>"; # Disconnect from the database $dbh->disconnect();

Also, make sure that you use:
#!/usr/bin/perl -w use strict;
Use the search at the top for additional help on those and other topics (it's been discussed numerous times.) Also see the node: How to RTFM it's a really good insite to finding Perl help.
HTH

- Mission Updated: fixed my typos (again.)

Replies are listed 'Best First'.
Re: Re: connecting Perl with MS SQL Server on NT using ODBC
by x-c-ute-o-ner (Initiate) on Nov 17, 2002 at 07:27 UTC
    Excuse me... I tried many time using your sample code for ODBC, but there is an error everytime i tried to use this code:
    #!/usr/bin/perl -w use strict; use DBI; # Module connection for ODBC # Set up variables for the connection my $server_name = "shadow"; my $server_ip = "10.207.130.8:8080"; my $database_name = "Smart Response System"; my $database_user = "cs59173"; my $database_pass = "5055"; my $DSN = "driver={SQL Server};server=$server_name;tcpip=$server_ip;da +tabase=$database_name;uid=$database_user;pwd=$database_pass"; my $DBH = DBI->connect("DBI:ODBC:$DSN") or die "Couldn't open database +: $DBI::errstr\n";
    The error message is:

    DBI->connect(driver={SQL Server};server=shadow;tcpip=10.207.130.8;database=Smart Response System;uid=cs59173;pwd=5055) failed: MicrosoftODBC Driver Manager Invalid string or buffer length (SQL-S1090)(DBD: db_login/SQLConnect err=-1) at c:\Perl\SRS\sql.pl line 13

    FIY, i have tried also sequence of examples by other monks, but also failed. Could you please help tracing the problems? Thanx a lot...
      x-c-ute-o-ner,

      Sorry that I don't see an obvious error in this code. I do have a couple of questions that might help your debugging. Just look through the list and make sure you have the correct information.

      • The conection is expecting a MS SQL server connection, is that your correct database type?
      • Did you try to re-install the DBI.pm (module) and DBD-ODBC.pm (driver) on the server and on your testing machine using PPM?
      • Are you certain of the IP?
      • Did you double check your Username, PWD?
      • Is the database name correct? (BTW: None of my databases have spaces in their names. That doesn't mean that it won't work like that, but I've not tried that before. You may want to rename you database to something without spaces.)

      I don't see an error, and those questions are just to get you thinking on the debugging process. I hope you have good luck on this problem. If there is any progress, or different problem, please make sure you post the final solution, or additional problems.

      Thanks,


      - Mission
      Not sure if its too late to reply this post, but always useful for current users facing similar problem.

      Scenario 1: If DB connection is not successful while running perl code from cmd, please make sure you have perl driver installed.

      Scenario 2: If DB connect is successful while running perl code from cmd and getting failed from Apache, please make sure to have appropriate driver in System DSN. Driver will depend upon DB you are trying to connect.