faqts : Computers : Programming : Languages : Perl : Database Backed Sites : Microsoft SQL Server

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

27 of 32 people (84%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Perl: Database: Microsoft SQL server: Connect: How access Microsoft SQL server Northwind database?

Dec 27th, 2004 16:55
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 28 December 2004 - 01:46 am -------------------

Perl: Database: Microsoft SQL server: Connect: How access Microsoft 
SQL server Northwind database?

---

Steps: Overview:

 1. -Make sure Microsoft SQL server is installed on your computer

 2. -Make sure Perl is installed on your computer

     (1. Note that I used ActiveState Perl v5.6
        (thus the older version), because of
        severe problems with the latest build v5.8)

 3. -Install DBD-ODBC in Perl

 4. -Register your Microsoft SQL server database
     for ODBC use

 5. -Run the example script which connects to
     your Microsoft SQL server Northwind database
     and shows some data

     1. -Save this Perl script in a file

          e.g.

           yourtest.pl

--- cut here: begin --------------------------------------------------

#!c:\perl\bin\perl.exe
# Program to query a database and display the contents in a table

use warnings;

use strict;

use DBI;

use DBD::ODBC;

my $dbh = DBI->connect( "DBI:ODBC:Northwind", "sa", "" ) or
   die( "Could not make connection to database: $DBI::errstr" );

my $sth = $dbh->prepare( q{ SELECT EmployeeId, FirstName FROM 
Employees } ) or
   die( "Cannot prepare statement: ", $dbh->errstr(), "\n" );

my $rc = $sth->execute() or
   die( "Cannot execute statement: ", $sth->errstr(), "\n" );

my @array;

while ( @array = $sth->fetchrow_array() ) {
   write();
}

# Check to see if fetch terminated early
warn( $DBI::errstr ) if $DBI::err;

$sth->finish();

$dbh->disconnect();

format STDOUT =
@<<<<<<@<<<<<<<<<
$array[ 0 ], $array[ 1 ]
.

--- cut here: end ----------------------------------------------------

 6. Run your Perl script

    1. On the MSDOS command line type the following command

        perl <your filename>

         e.g.

          perl yourtest.pl

       (make sure that perl.exe is in your MSDOS PATH,
        and adapt the filepath of test.pl so that it
        is found)

    2. It should you show then output in your
       MSDOS box similar to the following:

--- cut here: begin --------------------------------------------------

        > perl c:\temp\yourtest.pl

        1      Nancy
        2      Andrew
        3      Janet
        4      Margaret
        5      Steven
        6      Michael
        7      Robert
        8      Laura
        9      Anne

--- cut here: end ----------------------------------------------------

---
---

Tested successfully on
-Microsoft Windows XP Professional,
 running
 -Microsoft SQL Server 2000
 -ActiveState Perl v5.6.1.630

---
---

Book: see also:

---

[book: author=Deitel, H. M. / Deitel, P. J. / Nieto, T. R. / McPhie, 
D. C. - title=Perl How to Program - publisher=Prentice Hall - 
year=2001 - ISBN= 0130284181 - p. 491 'Working with DBI']

---
---

Internet: see also:

---

Perl: Database: Link: Overview: Can you give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/33044/fid/654

----------------------------------------------------------------------