Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guru's, I am wanting to connect to a Microsoft SQL 2000 Database from a windows XP platform using Active Perl. What is the easiest way to achieve this connection. I have been reading on this topic for considerable time and would like the advise of a proven easy to use technique (DBI, DBD, Sybase etc etc). What modules are required and adjustments needed. Also do i need to add software on the external SQL server. Any advise would be greatly appreciated. Thanks Daniel

Replies are listed 'Best First'.
Re: Perl connect to Microsoft SQL 2000
by tadamec (Beadle) on Jun 30, 2004 at 06:07 UTC

    DBI and DBD are really one in the same. They're also the de facto standard for Perl database interaction.

    You probably want to look at DBI and DBD::ADO, as it works really well for Win32 connections. As a bonus, you won't have to install any Sybase open client stuff, either. You'll need some Win32 like Win32::OLE if you don't have them already.

    If you're wanting to branch out under some *nix OS, I'd take a look at DBD::Sybase using FreeTDS. Keep in mind that FreeDTS and DBD::Sybase have some problems with placeholder values, so you'll probably have to change some of your statement parsing code. I don't really like the way that the Sybase client libraries work, but I've heard they work equally well.

    As for special software, you'll need Perl, the modules that DBD::ADO requires (they're listed in the POD) and the correct ADO drivers for your platform (available from Microsoft). It's about the same as deploying any other Win32 database application, actually.

    The only part that I don't really understand is the "adjustments needed" statement. Adjustments from what? Are you a DBA who's only ever used the SQL2000 admin interface? A seasoned Java programmer? An old hat at Visual Basic 5.0? An Access form tweaker? Each of these situations will probably impose different requirements, both in language and data access methodologies. If you care to elaborate a bit more for what you're looking for, I'll post a follow-up.

    (EDIT: added the third paragraph because I didn't think I was sufficiently long-winded.)

      I don't really like the way that the Sybase client libraries work, but I've heard they work equally well.
      The Sybase libs will NOT work against MS-SQL 2k, because MS changed the login sequence so the fallback to the TDS 4.2 protocol fails (TDS 4.2 is supposed to be the lowest common denominator in the TDS protocol stack, and should be supported by both Sybase and MS.)

      BTW - what don't you like with the Sybase libs?

      Update: dragonchild /msg'd me to say that this node was a bit confusing. What I mean is that the Sybase Client Libraries can't be used to connect to MS-SQL 2k. However, FreeTDS works fine (with some limitations), so DBD::Sybase can certainly be used when built with the FreeTDS libraries.

      Michael

Re: Perl connect to Microsoft SQL 2000
by EdwardG (Vicar) on Jun 30, 2004 at 07:44 UTC

    This might get you jump started

    # See http://perlmonks.org/index.pl?node_id=183417 package DBC; use DBI; my $dbh; sub new { return __PACKAGE__ } sub connect { my ($pointless, $details) = @_; my $server = $details->{server} || 'default_server'; my $database = $details->{database} || 'default_database'; my $user = $details->{user} || 'default_user'; my $password = $details->{password} || 'default_password'; my $DSN = "driver=\{SQL Server\}; Server=$server; database=$database; uid=$user; pwd=$password;"; $dbh ||= DBI->connect("dbi:ODBC:$DSN") or die "$DBI::errstr\n"; } 1;

    And now, when you need connectivity -

    use DBC; my $dbh = DBC->connect({ database => 'MyDB', user => 'me', password => 'psw' }); my $sql = qq{ select mycol, myothercol from mytable where myothercol = 'foo' }; my $qr = $dbh->selectall_arrayref($sql)

     

Re: Perl connect to Microsoft SQL 2000
by simon.proctor (Vicar) on Jun 30, 2004 at 10:45 UTC
    The easiest method to do this is to use DBD::ODBC and create a system DSN. Simply provide the DSN name along with username and password to the DBI and you have your DB object to play with.

    The advantage, then, is that when you go live you can create a system dsn on the live machine(s) that has the same name but points to your production db. You also don't have to embed the server IP or database name in your code. They are in the DSN.

    It also means no code changes are required unless your u/n, p/w or the dsn name changes.
Re: Perl connect to Microsoft SQL 2000
by Anonymous Monk on Jun 30, 2004 at 12:40 UTC
    Thankyou very much for your replies.. I failed to mention that i am a BIT student doing my final project. I have read about the DBI module (mysql and msql). The installation of this module for my required purpose has proven difficult, as bug fixes to prevent the different error messages have left me confused. Thankyou for the advise about the ODBC module also. If possible could a Guru outline the steps required for their suggested connection technologdy (for both Server and Client) in order to achieve my long awaited for "Hello World" from the Database. Or at least tell me what to read or do. Thankyou. Daniel ps - want to show my tutor that perl can do anything
      What errors are you getting?
      For work I recently had to write a small perl script to connect to MSSQL2K servers with active state. To install all the modules needed to connect I use:
      c:\> ppm install DBD-ODBC
      Those are for the client only, the server shouldn't need any setup beyond a login to connect with.
      To connect I use:
      #$winauth is set from the command line to force 'Windows Authenticatio +n' my $auth = $winauth ? 'Trusted_Connection=yes' : "uid=$username;pwd=$p +assword;"; my $DSN = "driver={SQL Server};Server=$server;database=$database;$auth +"; my $dbh = DBI->connect("dbi:ODBC:$DSN") || die "Error: $DBI::errstr";
Perl connect to Microsoft SQL 2000
by Anonymous Monk on Jul 01, 2004 at 03:00 UTC
    Do you advise using the Win32::ODBC module. We created a System DSN for our Microsoft SQL 2000 Server which is located externally on the network. In the examples i have read, they assume that the Server was located locally. This is the code i have been using: use Win32::ODBC; my($db) = new Win32::ODBC("dsn=$dsn_name; uid=$username; pwd=$password") || die "Could not connect to database\n"; $db->Sql("SELECT * FROM Tbl_Job"); while ($db->FetchRow()) { print "Hello World\n"; } Thanks again for your help. Daniel
      Do you advise using the Win32::ODBC module.
      If you have some very specific ODBC things you want to accomplish that aren't part of most everyday database interactions, then yes, use Win32::ODBC. Otherwise, use DBI with DBD::ODBC or one of the other DBDs recommended above. It can use either system DSNs or "DSN-less connections". It will be much easier to learn in the long run (more people to give advice), and what you learn about DBI will apply to many other database contexts whereas what you learn about Win32::ODBC only helps you with Win32/ODBC.