Carregando dados dum banco com ASP

O código abaixo se conecta a um banco Access.
A string DSBanco armazena o local do arquivo MDB.
DSConexao é a string de conexão. Cada gerenciador de banco de dados requer uma string de conexão específica.


conexao.asp
<%
'on error _resume next
dim Conexao, Erro

Conexao= null
Err.Clear

sub AbrirConexao
   'on error _resume next
   dim DSBanco, DSConexao

   DSBanco    = "C:\Inetpub\wwwroot\teste\banco.mdb;"
   DSConexao  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DSBanco & "Persist Security Info=False"
   Erro       = ""
   set Conexao= CreateObject ("ADODB.Connection")
   if Err.Number <> 0 then
      Erro= "[1] Erro ao criar o objeto de conexão." & " [" & Err.Number & "; " & Err.Description & "]"
   else
      Conexao.Open DSConexao
      if Err.Number <> 0 then
         Erro= "[2] Erro ao abrir a conexão com banco." & " [" & Err.Number & "; " & Err.Description & "]"
      end if
   end if
end sub

sub FecharConexao
   if Conexao.State = 1 then 'ADStateOpen
      Conexao.Close
   end if
   set Conexao= nothing
   Conexao    = null
end sub
%>

default.vb.asp
<!--#include file="conexao.asp"-->
<%
dim RS

sub Carregar
  if Erro = "" then
    set RS= Server.CreateObject ("ADODB.Recordset")
    RS.Open "select * from tb_link", Conexao
  end if
end sub

sub Fechar
  if RS.State = 1 then 'ADStateOpen
    RS.Close
  end if
  set RS= nothing
end sub

sub Listar
  AbrirConexao
  Carregar
  if (Erro = "") and not RS.EOF then
    RS.MoveFirst
    while not RS.EOF
      Response.Write "<tr>"
        Response.Write "<td>" & RS ("Nome")     & "</td>"
        Response.Write "<td>" & RS ("Endereco") & "</td>"
      Response.Write "</tr>"
      RS.MoveNext
    wend
  else
    Response.Write "<tr><td colspan=""2"">" & Erro & "</td></tr>"
  end if
  Fechar
  FecharConexao
end sub
%>

default.asp
<!--#include file="default.vb.asp"-->
<html>
   <head>
      <title>Conexão</title>
      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="expires" content="0">
   </head>
   <body>
      <table width="100%">
         <tr>
            <td><b>Nome</b></td>
            <td><b>Endereço</b></td>
         </tr>
         <% Listar %>
      </table>
   </body>
</html>


Clique aqui pra baixar o código

Clique aqui pra abrir no navegador




http://transeberiano.brinkster.net