[Cascavel-pm] Conectar o SQL Server

Solli Moreira Honorio shonorio em terra.com.br
Segunda Fevereiro 14 21:35:18 PST 2005


Rafael, 

Estou enviando uma fração do meu código base para acessar o SQL Server, tome o cuidado (e faça as alterações necessárias) com as constantes que estão no código.

O código acessa o SQL via Ado, e está com as melhores práticas de acesso neste componente.

Se você tiver interessado em ver as 1001 connection strings para OLEDB, dê uma olhada no link http://www.able-consulting.com/MDAC/ADO/Connection/OLEDB_Providers.htm

Solli M. Honorio

<code>
use Win32::OLE qw(in);
use Win32::OLE::Const;
sub ConnectDB
{
    my $DBConn        = Win32::OLE->new('ADODB.Connection');
    my $DB_CONNECTION = shift;
    
    $DBConn->Open( $DB_CONNECTION );
    if ( Win32::OLE->LastError() )
    {
        my $Error = Win32::OLE->LastError();
 # Trate o erro aqui !!!
        return undef;
    }
    else
    {
        return $DBConn;
    }
}
sub DBExecute
{
    my ($DB_CONNECTION, $String, $ToReturn, $ErrosReturn ) = @_;
    my ($DBConn, $RS, $ArgCount);
    my $Loop = 0;
    
    return INVALID_PARAMETER if ( ref ( $String ) );
    return NULL_PARAMETER    if (! $String );
    $ArgCount = ( scalar (@_) - 2 );                    # Get number of argument
    if ( $DBConn = ConnectDB($DB_CONNECTION) )          # Try to connect on DB
    {                                                   # ... if ok ...
        if (! $ArgCount )  # If has just one argument, then it's mean
        {                  # no recordset
            $DBConn->Execute ( $String, undef, (0x00000080 + 0x0001) );
        }
        else              # Otherwise mean recordset
        {
            $RS = $DBConn->Execute ( $String );
        }
        if ( Win32::OLE->LastError() )
        {
         # If any error happen, write error message on EventLog
         # and abort this class.
            my $Error = Win32::OLE->LastError();
            WriteEventLog ( $ENV{COMPUTERNAME},    # Computer name
                            ERROR_DB_EXCUTE,       # Event ID
                            $Error,                # String
                            EVT_ERROR);            # Event Type
            return ERROR_DB_EXCUTE;
        }
        if (! $ArgCount ) # Return ok if don't need anything more
                { $DBConn->Close(); return };
        # Clear any before data on an array ref.
        map { pop @{$ToReturn} } @{$ToReturn};
        # Save the result on array.
        while ( )
        {
            # Get all message returned by ADO connection
            foreach my $Error ( in ( $DBConn->Errors() ) )
            {
                my $Description = $Error->{Description};
                $Description    =~ s/\s*$//;
                push @{$ErrosReturn}, $Description;
            }
    
            if ( defined $RS->{EOF} )
            {
                while (! $RS->{EOF} )
                {
                    my %hash = ();
                    foreach my $field ( in ( $RS->Fields() ) )
                    {
                        $hash{$field->{Name}} = $field->{Value}
                    }
                    push @{$ToReturn}, \%hash;
                    $RS->MoveNext;
                }
            }
            eval { $RS = $RS->NextRecordSet(); };
            last if ( $@ );
        }
        # Clean all object used by ADO connection...
        $RS->Close();
        $DBConn->Close();
        undef $RS;
        undef $DBConn;
        return;
    }
    else
    {
        return ERROR_DB_CONNECT;
    }
}
my $ConnectionString = "Provider=sqloledb; Data Source=Server; Initial Catalog=master;Integrated Security=SSPI";
my $string           = "select * from table";
DBExecute ($ConnectionString, $string, \@Return );
</code>

De:cascavel-pm-bounces em pm.org

Para:cascavel-pm em pm.org

Cópia:

Data:Mon, 14 Feb 2005 12:04:46 -0300

Assunto:[Cascavel-pm] Conectar o SQL Server

  

> Olá Monges,
>  
>   Alguém sabe me dizer se existe algum módulo para conectar ao SQL Server?
>  
> Desde já agradeço.
>  
> Um abraço,


Rafael Szarblewski - Sistêmica Computadores
Desenvolvedor Web
rafael em sistemica.info
Fone: (51)3328-11-22 
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: http://mail.pm.org/pipermail/cascavel-pm/attachments/20050215/1cc8636e/attachment-0001.htm


Mais detalhes sobre a lista de discussão Cascavel-pm