<DIV>
<DIV>Rafael, </DIV>
<DIV> </DIV>
<DIV>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.</DIV>
<DIV> </DIV>
<DIV>O código acessa o SQL via Ado, e está com as melhores práticas de acesso neste componente.</DIV>
<DIV> </DIV>
<DIV>Se você tiver interessado em ver as 1001 connection strings para OLEDB, dê uma olhada no link <A href="http://www.able-consulting.com/MDAC/ADO/Connection/OLEDB_Providers.htm">http://www.able-consulting.com/MDAC/ADO/Connection/OLEDB_Providers.htm</A></DIV>
<DIV> </DIV>
<DIV>Solli M. Honorio</DIV>
<DIV> </DIV>
<DIV><code></DIV>
<DIV>use Win32::OLE qw(in);<BR>use Win32::OLE::Const;</DIV>
<DIV>sub ConnectDB<BR>{<BR> my $DBConn = Win32::OLE->new('ADODB.Connection');<BR> my $DB_CONNECTION = shift;<BR> <BR> $DBConn->Open( $DB_CONNECTION );</DIV>
<DIV> if ( Win32::OLE->LastError() )<BR> {<BR> my $Error = Win32::OLE->LastError();<BR> # Trate o erro aqui !!!<BR> return undef;<BR> }<BR> else<BR> {<BR> return $DBConn;<BR> }<BR>}</DIV>
<DIV>sub DBExecute<BR>{<BR> my ($DB_CONNECTION, $String, $ToReturn, $ErrosReturn ) = @_;<BR> my ($DBConn, $RS, $ArgCount);<BR> my $Loop = 0;<BR> <BR> return INVALID_PARAMETER if ( ref ( $String ) );<BR> return NULL_PARAMETER if (! $String );</DIV>
<DIV> $ArgCount = ( scalar (@_) - 2 ); # Get number of argument<BR> if ( $DBConn = ConnectDB($DB_CONNECTION) ) # Try to connect on DB<BR> { # ... if ok ...<BR> if (! $ArgCount ) # If has just one argument, then it's mean<BR> { # no recordset<BR> $DBConn->Execute ( $String, undef, (0x00000080 + 0x0001) );<BR> }<BR> else # Otherwise mean recordset<BR> {<BR> $RS = $DBConn->Execute ( $String );<BR> }</DIV>
<DIV> if ( Win32::OLE->LastError() )<BR> {<BR> # If any error happen, write error message on EventLog<BR> # and abort this class.<BR> my $Error = Win32::OLE->LastError();<BR> WriteEventLog ( $ENV{COMPUTERNAME}, # Computer name<BR> ERROR_DB_EXCUTE, # Event ID<BR> $Error, # String<BR> EVT_ERROR); # Event Type<BR> return ERROR_DB_EXCUTE;<BR> }</DIV>
<DIV> if (! $ArgCount ) # Return ok if don't need anything more<BR> { $DBConn->Close(); return };</DIV>
<DIV> # Clear any before data on an array ref.<BR> map { pop @{$ToReturn} } @{$ToReturn};<BR> # Save the result on array.<BR> while ( )<BR> {<BR> # Get all message returned by ADO connection<BR> foreach my $Error ( in ( $DBConn->Errors() ) )<BR> {<BR> my $Description = $Error->{Description};<BR> $Description =~ s/\s*$//;<BR> push @{$ErrosReturn}, $Description;<BR> }<BR> <BR> if ( defined $RS->{EOF} )<BR> {<BR> while (! $RS->{EOF} )<BR> {<BR> my %hash = ();<BR> foreach my $field ( in ( $RS->Fields() ) )<BR> {<BR> $hash{$field->{Name}} = $field->{Value}<BR> }<BR> push @{$ToReturn}, \%hash;<BR> $RS->MoveNext;<BR> }<BR> }<BR> eval { $RS = $RS->NextRecordSet(); };<BR> last if ( $@ );<BR> }</DIV>
<DIV> # Clean all object used by ADO connection...<BR> $RS->Close();<BR> $DBConn->Close();<BR> undef $RS;<BR> undef $DBConn;<BR> return;<BR> }<BR> else<BR> {<BR> return ERROR_DB_CONNECT;<BR> }<BR>}</DIV>
<DIV>my $ConnectionString = "Provider=sqloledb; Data Source=Server; Initial Catalog=master;Integrated Security=SSPI";<BR>my $string = "select * from table";</DIV>
<DIV>DBExecute ($ConnectionString, $string, \@Return );<BR></code></DIV>
<DIV> </DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR>
<TD class=txt width=100 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2><B>De:</B></FONT></TD>
<TD class=txt width=400 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2>cascavel-pm-bounces@pm.org</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR>
<TD class=txt width=100 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2><B>Para:</B></FONT></TD>
<TD class=txt width=400 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2>cascavel-pm@pm.org</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR>
<TD class=txt width=100 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2><B>Cópia:</B></FONT></TD>
<TD class=txt width=400 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR>
<TD class=txt width=100 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2><B>Data:</B></FONT></TD>
<TD class=txt width=400 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2>Mon, 14 Feb 2005 12:04:46 -0300</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR>
<TD class=txt width=100 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2><B>Assunto:</B></FONT></TD>
<TD class=txt width=400 bgColor=#d8d8d8><FONT face="Trebuchet MS; verdana; arial" size=2>[Cascavel-pm] Conectar o SQL Server</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR>
<TD width=100 bgColor=#e8e8e8> </TD>
<TD width=400 bgColor=#e8e8e8> </TD></TR></TBODY></TABLE></DIV>
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<STYLE></STYLE>
<DIV>> <FONT face=Tahoma size=2>Olá Monges,</FONT></DIV>
<DIV>> <FONT face=Tahoma size=2></FONT> </DIV>
<DIV>> <FONT face=Tahoma size=2> Alguém sabe me dizer se existe algum módulo para conectar ao SQL Server?</FONT></DIV>
<DIV>> <FONT face=Tahoma size=2></FONT> </DIV>
<DIV>> <FONT face=Tahoma size=2>Desde já agradeço.</FONT></DIV>
<DIV>> <FONT face=Tahoma size=2></FONT> </DIV>
<DIV>> <FONT style="FONT-SIZE: 11px; COLOR: #333333; FONT-FAMILY: Verdana,Tahoma,sans-serif">Um abraço,<BR>
<HR color=#ce0000 SIZE=1>
Rafael Szarblewski - Sistêmica Computadores<BR>Desenvolvedor Web<BR><A class=link href="mailto:rafael@sistemica.info">rafael@sistemica.info</A><BR>Fone: (51)3328-11-22 </FONT></DIV></DIV>