Ir para conteúdo
Fórum Script Brasil
  • 0

Desafio... Information_schema - Porque Não Funciona ? ? ?


Camisa

Pergunta

alguém saberia me dizer o porque que o código abaixo não funciona . . .

ele simplesmente retorna uma página em branco

<%
Set Conexao = Server.CreateObject("Adodb.Connection")
Conexao.ConnectionString = "DBQ=" & Server.MapPath("../../databases/123/123db.mdb") & ";"
Conexao.ConnectionString = Conexao.ConnectionString & "Driver={Microsoft Access Driver (*.mdb)};uid=Admin; password=XXXXXXX"
Conexao.Open


Sub RenderTableConstraints(strTableName, conexao)

  Dim strQuery
  Dim strConstraintName, strConstraintType, strConstraintDetails
  Dim oRsConstraintList
  
  '--- Get the list of constraints on the table.
  strQuery = "SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='" + strTableName + "'"
  Set oRsConstraintList = conexao.Execute(strQuery)

  Response.Write("<BR><BR><B>Constraints</B>")
    
  '--- Start a table for the list of columns.

  Response.Write("<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 WIDTH=100% Class=Normal bordercolor=Navy>")

  Response.Write("<TR>")
  Response.Write("<TD WIDTH='25%'>Constraint Type</TD>")
  Response.Write("<TD WIDTH='35%'>Constraint Name</TD>")
  Response.Write("<TD WIDTH='40%'>Fields(PK,FK) / Clause(CHECK)</TD>")
  Response.Write("</TR>")
  
  '--- Loop to parse to the list of columns.
  While (Not oRsConstraintList.EOF)
  
    '--- Store the constraint name and type.
    strConstraintName = oRsConstraintList("CONSTRAINT_NAME")
    strConstraintType = oRsConstraintList("CONSTRAINT_TYPE")
    
    '--- Get constraint details based on the constraint type.
    If (strConstraintType = "CHECK") Then
      strConstraintDetails = GetClauseForConstraint(strConstraintName, conexao)
    Else
      strConstraintDetails = GetColumnsForConstraint(strConstraintName, strTableName, conexao)
    End If
  
    '--- Output the details of each column.
    Response.Write("<TR>")
    Response.Write("<TD>" + strConstraintType + "</TD>")
    Response.Write("<TD>" + strConstraintName + "</TD>")
    Response.Write("<TD>" + strConstraintDetails + "</TD>")
    Response.Write("</TR>")    
     
    '--- Move to the next column.
    oRsConstraintList.MoveNext
  Wend
    
  '--- End the table for columns.
  Response.Write("</TABLE><br><br>")
  
  '--- Clean up.
  Set oRsConstraintList = Nothing
End Sub

Function GetClauseForConstraint(strConstraintName, conexao)

  Dim strQuery
  Dim oRs
  
  '--- Build query to get the column list for constraint.
  strQuery = "SELECT c.CHECK_CLAUSE " + _
             "FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS c " + _
             "WHERE ( c.constraint_name = '" + strConstraintName + "')"
    
  '--- Use connection and get the recordset.
  Set oRs = conexao.Execute(strQuery)
  
  '--- Return the string.
  GetClauseForConstraint = oRs("CHECK_CLAUSE")
  
End Function


'------------------------------------------------------------------
'--- To get the list of cols associated with constraint as , delimited str.
'------------------------------------------------------------------
Function GetColumnsForConstraint(strConstraintName, strTableName, conexao)

  Dim iCount
  Dim strQuery, strFldNames
  Dim oRs
  

  '--- Build query to get the column list for constraint.
  strQuery = "SELECT d.column_name " + _
             "FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE d " + _
             "WHERE (d.constraint_name = '" + strConstraintName + " ') " + _
             "ORDER BY d.ordinal_position"
    
  '--- Use connection and get the recordset.
  Set oRs = conexao.Execute(strQuery)
  
  '--- Initialize the field names string.
  strFldNames = "": iCount = 0
  
  '--- Loop to build a comma delimited string.
  While (Not oRs.EOF)
    '--- Add a comma separator between 2 column names.
    If (iCount > 0) Then strFldNames = strFldNames + ", "
    
    '--- Add the next field name.
    strFldNames = strFldNames + oRs("column_name")
  
    '--- Move to next row and increment counter.
    oRs.MoveNext: iCount = iCount + 1
  Wend
  
  '--- Return the column names as a comma delimited string.
  GetColumnsForConstraint = strFldNames
  
End Function

conexao.Close
Set conexao = Nothing

%>

Obrigado

Marcelo Camisa

Link para o comentário
Compartilhar em outros sites

6 respostass a esta questão

Posts Recomendados

  • 0

este código ( segundo a fonte ) serveria para me retornar o meu banco de dados...

tabela

nome_do_campo - tipo do campo

outro campo - tipo do outro campo...

e assim vai.

a unica coisa que preciso, é saber quais as tabelas que tenho em meu mdb e dentro destas tabelas, saber quais os campos e seus respectivos tipo ( varchar, autonumerico e assim por diante... )

Marcelo Camisa

Link para o comentário
Compartilhar em outros sites

  • 0

mas esse codigo não vai fazer nd se você não chamar a funcao. acho q tem q ser essa daqui:

Sub RenderTableConstraints(strTableName, conexao)

ai você passa, me parece, o nome da tabela e o objeto conexao.

mas não sei não, você ta usando bd access, não?? acho q esse codigo deve ser só pra sql.

Link para o comentário
Compartilhar em outros sites

  • 0
Guest --Camisa --

seguinte...

achei o seguinte código:

<%
Sub Main()

Dim strConnectString, strCurrentTableName, strQuery
Dim oConn
Dim oRsTableList

  '--- Retrieve the connection string from Querystring.
  strConnectString = Request.QueryString("strConnectString")

  If IsEmpty(strConnectString) Then
    strConnectString = "Driver={SQL Server}; Server=(local);database=pubs; UID=sa; PWD=www.topcoolsite.com"
  End If

  '--- Create the connection object.
  Set oConn = Server.CreateObject("ADODB.Connection")
  oConn.Open strConnectString

  '--- Get the list of tables within the database.
  strQuery = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'"
  Set oRsTableList = oConn.Execute(strQuery)

  '--- Loop within the list of tables.
  While (Not oRsTableList.EOF)

    '--- Get the table name.
    strCurrentTableName = oRsTableList("TABLE_NAME")  
  
    '--- Render the table fields.
    RenderTableAttributes strCurrentTableName, oConn
    
    '--- Render the table constraints.
    RenderTableConstraints strCurrentTableName, oConn
    
    '--- Next table.
    oRsTableList.MoveNext

  Wend

  Set oConn = Nothing

End Sub

Sub RenderTableAttributes(strTableName, oConn)

Dim strTemp, strQuery
Dim oRsColumnList
  
  '--- Get the list of columns/attributes/fields.
  strQuery = "  SELECT * FROM INFORMATION_SCHEMA.COLUMNS " + _
             "   WHERE TABLE_NAME='" + strTableName + "'" + _
             "ORDER BY ORDINAL_POSITION"
             
  Set oRsColumnList = oConn.Execute(strQuery)

  '--- Output the table header.
  Response.Write("<font size=+2>")
  Response.Write("Table: " + strTableName + "<br>")
  Response.Write("</font>")
  Response.Write("<br><b>Attributes</b>")
    
  Response.Write("<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 WIDTH=100% bordercolor=Navy>")
  Response.Write("<TR>")  
  Response.Write("<TD align=center WIDTH='10%'>Serial No</TD>")
  Response.Write("<TD WIDTH='30%'>Field Name</TD>")
  Response.Write("<TD WIDTH='10%'>Nullable?</TD>")
  Response.Write("<TD WIDTH='20%'>DataType/Width</TD>")
  Response.Write("<TD WIDTH='30%'>Default</TD>")  
  Response.Write("</TR>")
  
  '--- Loop to parse to the list of columns.
  While Not oRsColumnList.EOF
  
    '--- Output the details of each column.
    Response.Write("<TR>")

    Response.Write("<TD align=center>" + CStr(oRsColumnList("ORDINAL_POSITION")) + "</TD>")
    Response.Write("<TD>" + oRsColumnList("COLUMN_NAME") + "</TD>")
    Response.Write("<TD>" + oRsColumnList("IS_NULLABLE") + "</TD>")

    strTemp = oRsColumnList("DATA_TYPE")

    If (Not IsNull(oRsColumnList("CHARACTER_MAXIMUM_LENGTH"))) Then
      strTemp = strTemp + " (" + CStr(oRsColumnList("CHARACTER_MAXIMUM_LENGTH")) + ")"
    Else
      strTemp = strTemp + " (" + CStr(oRsColumnList("NUMERIC_PRECISION")) + ")"
    End If

    Response.Write("<TD>" + strTemp + "</TD>")    
   
    If (Not IsNull(oRsColumnList("COLUMN_DEFAULT"))) Then
      Response.Write("<TD>" + oRsColumnList("COLUMN_DEFAULT") + "</TD>")
    Else
      Response.Write("<TD> </TD>")
    End If
      
    Response.Write("</TR>")
     
    '--- Move to the next column.
    oRsColumnList.MoveNext

  Wend
    
  '--- End the table for columns.
  Response.Write("</TABLE>")    

  Set oRsColumnList = Nothing
End Sub 

Sub RenderTableConstraints(strTableName, oConn)

  Dim strQuery
  Dim strConstraintName, strConstraintType, strConstraintDetails
  Dim oRsConstraintList
  
  '--- Get the list of constraints on the table.
  strQuery = "SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='" + strTableName + "'"
  Set oRsConstraintList = oConn.Execute(strQuery)

  Response.Write("<BR><BR><B>Constraints</B>")
    
  '--- Start a table for the list of columns.

  Response.Write("<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 WIDTH=100% Class=Normal bordercolor=Navy>")

  Response.Write("<TR>")
  Response.Write("<TD WIDTH='25%'>Constraint Type</TD>")
  Response.Write("<TD WIDTH='35%'>Constraint Name</TD>")
  Response.Write("<TD WIDTH='40%'>Fields(PK,FK) / Clause(CHECK)</TD>")
  Response.Write("</TR>")
  
  '--- Loop to parse to the list of columns.
  While (Not oRsConstraintList.EOF)
  
    '--- Store the constraint name and type.
    strConstraintName = oRsConstraintList("CONSTRAINT_NAME")
    strConstraintType = oRsConstraintList("CONSTRAINT_TYPE")
    
    '--- Get constraint details based on the constraint type.
    If (strConstraintType = "CHECK") Then
      strConstraintDetails = GetClauseForConstraint(strConstraintName, oConn)
    Else
      strConstraintDetails = GetColumnsForConstraint(strConstraintName, strTableName, oConn)
    End If
  
    '--- Output the details of each column.
    Response.Write("<TR>")
    Response.Write("<TD>" + strConstraintType + "</TD>")
    Response.Write("<TD>" + strConstraintName + "</TD>")
    Response.Write("<TD>" + strConstraintDetails + "</TD>")
    Response.Write("</TR>")    
     
    '--- Move to the next column.
    oRsConstraintList.MoveNext
  Wend
    
  '--- End the table for columns.
  Response.Write("</TABLE><br><br>")
  
  '--- Clean up.
  Set oRsConstraintList = Nothing
End Sub

Function GetClauseForConstraint(strConstraintName, oConn)

  Dim strQuery
  Dim oRs
  
  '--- Build query to get the column list for constraint.
  strQuery = "SELECT c.CHECK_CLAUSE " + _
             "FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS c " + _
             "WHERE ( c.constraint_name = '" + strConstraintName + "')"
    
  '--- Use connection and get the recordset.
  Set oRs = oConn.Execute(strQuery)
  
  '--- Return the string.
  GetClauseForConstraint = oRs("CHECK_CLAUSE")
  
End Function


'------------------------------------------------------------------
'--- To get the list of cols associated with constraint as , delimited str.
'------------------------------------------------------------------
Function GetColumnsForConstraint(strConstraintName, strTableName, oConn)

  Dim iCount
  Dim strQuery, strFldNames
  Dim oRs
  

  '--- Build query to get the column list for constraint.
  strQuery = "SELECT d.column_name " + _
             "FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE d " + _
             "WHERE (d.constraint_name = '" + strConstraintName + " ') " + _
             "ORDER BY d.ordinal_position"
    
  '--- Use connection and get the recordset.
  Set oRs = oConn.Execute(strQuery)
  
  '--- Initialize the field names string.
  strFldNames = "": iCount = 0
  
  '--- Loop to build a comma delimited string.
  While (Not oRs.EOF)
    '--- Add a comma separator between 2 column names.
    If (iCount > 0) Then strFldNames = strFldNames + ", "
    
    '--- Add the next field name.
    strFldNames = strFldNames + oRs("column_name")
  
    '--- Move to next row and increment counter.
    oRs.MoveNext: iCount = iCount + 1
  Wend
  
  '--- Return the column names as a comma delimited string.
  GetColumnsForConstraint = strFldNames
  
End Function
%>
<HTML>
<HEAD>
</HEAD>
<BODY>

<%
  Call main
%>

</BODY>
</HTML>
ai, troquei a conexão dele pela minha:
era :
Dim strConnectString, strCurrentTableName, strQuery
Dim oConn
Dim oRsTableList

  '--- Retrieve the connection string from Querystring.
  strConnectString = Request.QueryString("strConnectString")

  If IsEmpty(strConnectString) Then
    strConnectString = "Driver={SQL Server}; Server=(local);database=pubs; UID=sa; PWD=www.topcoolsite.com"
  End If

  '--- Create the connection object.
  Set oConn = Server.CreateObject("ADODB.Connection")
  oConn.Open strConnectString

e ficou:

[COLOR=red]Dim strConnectString, strCurrentTableName, strQuery

Dim oRsTableList

  '--- Retrieve the connection string from Querystring.
  strConnectString = Request.QueryString("strConnectString")

  If IsEmpty(strConnectString) Then
'    nada
  End If

  '--- Create the connection object.
  Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = "DBQ=" & Server.MapPath("../../databases/xxx/xxx.mdb") & ";"
oConn.ConnectionString = oConn.ConnectionString & "Driver={Microsoft Access Driver (*.mdb)};uid=Admin; password=xxxxx"
  
  oConn.Open 'strConnectString[/COLOR]
ai que recebo o seguinte erro...:
Microsoft OLE DB Provider for ODBC Drivers error '80004005' 

[Microsoft][ODBC Microsoft Access Driver] [COLOR=red]Could not find file 'c:\windows\system32\inetsrv\INFORMATION_SCHEMA.mdb'.[/COLOR] 

/xxx/ver_table2.asp, line 25

cara, to perdidinho nisso aqui, nunca achei que fosse tão difícil saber quais os campos e seus tipos de dados do access via asp...

se alguém puder me ajudar, eu agradeço

Marcelo Camisa

Link para o comentário
Compartilhar em outros sites

  • 0

bom basicamente sua troca de conexão não foi feita....

c:\windows\system32\inetsrv\INFORMATION_SCHEMA.mdb

o arquivo não existe

to d mal humor quando você falar qual é o premio do desafio eu te ajudo com seu código,

o sql server é so banco de dados

uma coisa que não adianta é você não saber nada disso e querer que funcione... não rola mesmo... uma aprendizagem basica seria interresante...

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...