Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 8th, 2006, 01:18 AM
baseballdude_'s Avatar
baseballdude_ baseballdude_ is offline
Expert Learner
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2005
Location: Wisconsin
Posts: 1,878 baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)  Folding Points: 22104 Folding Title: Starter FolderFolding Points: 22104 Folding Title: Starter Folder
Time spent in forums: 1 Week 5 Days 11 h 55 m 55 sec
Reputation Power: 62
Send a message via AIM to baseballdude_ Send a message via MSN to baseballdude_ Send a message via Yahoo to baseballdude_ Send a message via Google Talk to baseballdude_
Wink Database Explorer

I've created this script to use as an administration tool for my own website. I've found it to be very handy. It's fully functional, the only thing that could be fixed (much could be added), is custom error messages.

Just fill in the constants and you're ready to go!
ASP Code:
Original - ASP Code
  1. <%
  2.  
  3. '=================================================  ==============================
  4. ' User Settings                                          Coded By: Dan Barnett
  5. '=================================================  ==============================
  6. Const intDatabaseType = 1      '1=MySQL, 2=Access, 3=SQL Server
  7. Const strServer = "localhost"      'Server URL or localhost
  8. Const strDatabase = "DBname"        'Database Name
  9. Const strUserID = "username"        'Database User ID
  10. Const strPassword = "password"    'Database Password
  11. Const strDefaultTable = "users"  'Default table for database explorer
  12. Const strDefaultColumn = "username" 'Default column for database explorer
  13. '-------------------------------------------------------------------------------
  14.  
  15. strScriptName = Request.ServerVariables("Script_Name")
  16.  
  17. If Session("loggedin") = True And Request.QueryString() <> "logout" Then
  18.  
  19. Dim strValue, strSQL, strLeft, strRight, strScriptName, Conn, RS
  20.     If intDatabaseType = 1 Then
  21.         strLeft = "`"
  22.         strRight = "`"
  23.     Else
  24.         strLeft = "["
  25.         strRight = "]"
  26.     End If
  27.  
  28. Set Conn = Server.CreateObject("ADODB.Connection")
  29. Set RS = Server.CreateObject("ADODB.Recordset")
  30. Conn.Open = SQLConnect(intDatabaseType, strServer, strDatabase, strUserID, strPassword)
  31.  
  32. If Request("query") = "" And Request.QueryString("sortby") = "" And Request.QueryString("order") = "" Then
  33.     strSQL = "SELECT * FROM " & strLeft & strDefaultTable & strRight & " ORDER BY " & strLeft & strDefaultColumn & strRight & " ASC;"
  34.     Session("columns") = " * "
  35.     Session("where") = " "
  36.     Session("table") = strDefaultTable
  37. ElseIf Request.Form("table") <> "" And Request.Form("table") <> "-1" Then
  38.     strSQL = "SELECT * FROM " & strLeft & Request.Form("table") & strRight & ";"
  39.     Session("columns") = " * "
  40.     Session("where") = " "
  41.     Session("table") = Request.Form("table")
  42. Else
  43.     If Request.QueryString("sortby") <> "" Or Request.QueryString("order") <> "" Then
  44.         Dim strSortBy, strOrder
  45.  
  46.         strSortBy = Request.QueryString("sortby")
  47.         strOrder = UCase(Request.QueryString("order"))
  48.  
  49.         If strSortBy = "" Then strSortBy = strDefaultColumn
  50.         If strOrder <> "ASC" Then
  51.             If strOrder <> "DESC" Then strOrder = "ASC"
  52.         End If
  53.  
  54.         strSQL = "SELECT" & Session("columns") & "FROM " & strLeft & Session("table") & strRight & Session("where") & "ORDER BY " & strLeft & strSortBy & strRight & " " & strOrder & ";"
  55.     Else
  56.         strSQL = Request("query")
  57.  
  58.         Dim intColumnPos, intTablePos, strTable, intWherePos
  59.         intColumnPos = InStr(strSQL, "FROM") - 7
  60.         Session("columns") = Mid(strSQL, 7, intColumnPos)
  61.  
  62.         If InStr(strSQL, " WHERE ") > 0 Then
  63.             intTablePos = InStr(strSQL, "FROM") + 5
  64.             strTable = Mid(strSQL, intTablePos)
  65.                 intTablePos = InStr(strTable, " ")
  66.                 Session("table") = StripDB(Left(strTable, intTablePos), 0)
  67.  
  68.             Dim intTablePos2, intOrderByPos
  69.  
  70.             intTablePos2 = InStr(strSQL, strLeft & Session("table") & strRight) + 7
  71.                 strSQL = Mid(strSQL, intTablePos2)
  72.             intOrderByPos = InStr(strSQL, "ORDER BY") - 1
  73.  
  74.             If InStr(strSQL, "ORDER BY") > 0 Then
  75.                 Session("where") = Mid(strSQL, 1, intOrderByPos)
  76.             Else
  77.                 intWherePos = InStrRev(StripDB(strSQL, 1), " ") + 1
  78.                 Session("where") = Mid(StripDB(strSQL, 1), intWherePos)
  79.             End If
  80.         Else
  81.             If InStr(strSQL, "ORDER BY") > 0 Then
  82.                 intTablePos = InStr(strSQL, "FROM") + 5
  83.                 strTable = Mid(strSQL, intTablePos)
  84.                     intTablePos = InStr(strTable, " ")
  85.                     Session("table") = StripDB(Left(strTable, intTablePos), 0)
  86.             Else
  87.                     intTablePos = InStrRev(StripDB(strSQL, 1), " ") + 1
  88.                     Session("table") = Mid(StripDB(strSQL, 1), intTablePos)
  89.             End If
  90.  
  91.             Session("where") = " "
  92.         End If
  93.  
  94.         strSQL = Request("query")
  95.     End If
  96. End If
  97. %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  98. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  99.     <head>
  100.         <title>Database Explorer</title>
  101.         <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
  102.         <meta http-equiv="Content-Language" content="en-us">
  103.         <style type="text/css">
  104.             a:link, a:visited, a:active { color: blue; }
  105.             a:hover { color: red; }
  106.             div { text-align: center; margin-bottom: 10px; }
  107.             table   { border-collapse: collapse; border: 2px solid #000; }
  108.             td, th  { border : 1px solid #000; }
  109.             th  { background: #b0c4de; }
  110.             input   { width: 50%; }
  111.             h2  { margin-top: 0; margin-bottom: 0; text-align: center; }
  112.         #logout { position: absolute; left: 3px; top: 3px; width: 10px; }
  113.             .italictext { font-style: italic; }
  114.         </style>
  115.     </head>
  116.     <body>
  117.     <div id="logout">[<a href="<%=strScriptName%>?logout" title="Logout">Logout</a>]</div>
  118.         <h2>Exploring Database <%=strDatabase%></h2>
  119.         <div>
  120.             <form method="post" action="<%=strScriptName%>">
  121.                 Query: <input type="text" name="query" value="<%=strSQL%>"> <button type="submit">Submit</button><br />
  122.                 - OR -<br />
  123.                 SELECT * FROM <select name="table">
  124.                     <option class="italictext" selected="selected" value="-1">Select One</option>
  125. <%
  126.     RS.Open Conn.OpenSchema(20)
  127.         While Not RS.EOF
  128.             Response.Write(Tab(5) & "<option value=""" & RS("TABLE_NAME") & """>" & RS("TABLE_NAME") & "</option>" & vbCrLf)
  129.             RS.MoveNext
  130.         Wend
  131.     RS.Close
  132. %>
  133.                 </select> <button type="submit">Submit</button>
  134.             </form>
  135.         </div>
  136.  
  137.         <hr />
  138.  
  139.         <table>
  140.             <tr>
  141. <%
  142. RS.Open strSQL, Conn
  143.     For Each x In RS.Fields
  144.         Dim strOrder2, strName
  145.         strName = x.Name
  146.         strOrder2 = "ASC"
  147.             If Request.QueryString("sortby") = strName And Request("order") = "ASC" Then strOrder2 = "DESC"
  148.  
  149.         Response.Write(Tab(4) & "<th><a href=""" & strScriptName & "?sortby=" & strName & "&order=" & strOrder2 & """ title=""Sort by column " & strName & """>" & strName & "</a></th>" & vbCrLf)
  150.     Next
  151.  
  152.     Response.Write(Tab(3) & "</tr>" & vbCrLf)
  153.  
  154.     While Not RS.EOF
  155.         Response.Write(Tab(3) & "<tr>" & vbCrLf)
  156.         For Each x In RS.Fields
  157.             strValue = x.Value
  158.             If strValue = "" Or IsNull(strValue) Then
  159.                 If strValue = "" Then
  160.                     strValue = "<span class=""italictext"">Empty</span>"
  161.                 Else
  162.                     strValue = "<span class=""italictext"">Null</span>"
  163.                 End If
  164.             Else
  165.                 strValue = StripHTML(strValue)
  166.             End If
  167.  
  168.             Response.Write(Tab(4) & "<td>")
  169.             If CInt(Len(strValue)) > 100 Then
  170.                 Response.Write(Left(strValue, 97) & "...")
  171.             Else
  172.                 Response.Write(strValue)
  173.             End If
  174.             Response.Write("</td>" & vbCrLf)
  175.         Next
  176.         Response.Write(Tab(3) & "</tr>" & vbCrLf)
  177.  
  178.         RS.MoveNext
  179.     Wend
  180. RS.close
  181. %>
  182.         </table>
  183.     </body>
  184. </html>
  185. <%
  186. Set RS = Nothing
  187. Set Conn = Nothing