home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / db_dsn.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  12.6 KB  |  323 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <!--
  14. The following include file contains all the VB ADO Constants.
  15. If you don't want to include the whole file, you can replace all
  16. constants with their numerical equivalents and the script will work the
  17. same way.  You could also cut and paste just the declarations you use.
  18. We simply got tired of looking up constants since we use so many!
  19. The file is included with VBScript so you should have it somewhere.
  20. If you can't find it, the full path so you can download the file is
  21. http://www.asp101.com/samples/adovbs.inc
  22. -->
  23. <!-- #INCLUDE FILE="./adovbs.inc" -->
  24. <%
  25. ' BEGIN USER CONSTANTS
  26. ' To just use a DSN, the format is shown on the next line:
  27. 'Const DSN_NAME = "DSN=ASP101email"
  28.  
  29. ' Two other samples I used it with.  Left in as syntax examples for DSN-less connections
  30. 'Const DSN_NAME = "DBQ=C:\InetPub\wwwroot\asp101\samples\database.mdb;Driver={Microsoft Access Driver (*.mdb)};"
  31. 'Const DSN_NAME = "DBQ=C:\InetPub\database\donations.mdb;Driver={Microsoft Access Driver (*.mdb)};"
  32.  
  33. Dim DSN_NAME
  34. ' Used this ODBC connection until 11/8/99
  35. 'DSN_NAME = "DBQ=" & Server.MapPath("db_dsn.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
  36.  
  37. ' Switched to OLE DB on 11/8/99 - giving it a try
  38. DSN_NAME = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("db_dsn.mdb") & ";"
  39.  
  40. Const DSN_USER = "admin"
  41. Const DSN_PASS = ""
  42. ' Ok, I know these are poorly named constants, so sue me!
  43. ' This script can be used without actually setting up a DSN, so
  44. ' DSN_NAME as well as the other two constants should really be named
  45. ' something more generic like CONNECTION_STRING, CONNECTION_USER, and
  46. ' CONNECTION_PASS, but I did it this way without really thinking about
  47. ' it and I'm too lazy to change it now.  If it bothers you, you do it!
  48. ' END USER CONSTANTS
  49.  
  50. ' BEGIN SUBS & FUNCTIONS SECTION
  51. Sub OpenConnection
  52.     Set objDC = Server.CreateObject("ADODB.Connection")
  53.     objDC.ConnectionTimeout = 15
  54.     objDC.CommandTimeout = 30
  55.     objDC.Open DSN_NAME, DSN_USER, DSN_PASS
  56. End Sub
  57.  
  58. Sub OpenRecordset(sType)
  59.     Dim sSqlString ' as String - building area for SQL query
  60.     Dim sCritOperator ' as String - basically "=" or "LIKE"
  61.     Dim sCritDelimiter ' as String - parameter delimiter "", "'", or "#"
  62.  
  63.     Set objRS = Server.CreateObject("ADODB.Recordset")
  64.     Select Case sType
  65.         Case "ListTables" ' Open RS of the Tables in the DB
  66.             Set objRS = objDC.OpenSchema(adSchemaTables)
  67.         Case "ViewTable"  ' Open the Selected Table
  68.             Set objRS = Server.CreateObject("ADODB.Recordset")
  69.             objRS.Open "[" & sTableName & "]", objDC, adOpenForwardOnly, adLockReadOnly
  70.         Case "DrillDown"  ' Open the Recordset built by the selected options            
  71.             Set objRS = Server.CreateObject("ADODB.Recordset")
  72.             
  73.             ' Build Our SQL Statement
  74.             sSqlString = "SELECT * FROM [" & sTableName & "]"
  75.             
  76.             ' If we're limiting records returned - insert the WHERE Clause into the SQL
  77.             If sCritField <> "" Then
  78.                 ' Figure out if we're dealinh with Numeric, Date, or String Values
  79.                 Select Case iCritDataType
  80.                     Case adSmallInt, adInteger, adSingle, adDouble, adDecimal, adTinyInt, adUnsignedTinyInt, adUnsignedSmallInt, adUnsignedInt, adBigInt, adUnsignedBigInt, adBinary, adNumeric, adVarBinary, adLongVarBinary, adCurrency, adBoolean
  81.                         sCritOperator = "="
  82.                         sCritDelimiter = ""
  83.                     Case adDate, adDBDate, adDBTime, adDBTimeStamp
  84.                         sCritOperator = "="
  85.                         sCritDelimiter = "#"
  86.                     Case adBSTR, adChar, adWChar, adVarChar, adLongVarChar, adVarWChar, adLongVarWChar
  87.                         sCritOperator = "LIKE"
  88.                         sCritDelimiter = "'"
  89.                 End Select
  90.                 sSqlString = sSqlString & " WHERE [" & sCritField & "] " & sCritOperator & " " & sCritDelimiter & sCritValue & sCritDelimiter
  91.             End If
  92.  
  93.             ' If we're sorting - insert the ORDER BY clause
  94.             If sSortOrder <> "none" Then
  95.                 sSqlString = sSqlString & " ORDER BY [" & sSortField & "] " & sSortOrder
  96.             End If
  97.  
  98.             sSqlString = sSqlString & ";"
  99.  
  100.             ' Open the actual Recordset using a Forward Only Cursor in Read Only Mode
  101.             objRS.Open sSqlString, objDC, adOpenForwardOnly, adLockReadOnly
  102.     End Select
  103. End Sub
  104.  
  105. Sub CloseRecordset
  106.     objRS.Close
  107.     Set objRS = Nothing
  108. End Sub
  109.  
  110. Sub CloseConnection
  111.     objDC.Close
  112.     Set objDC = Nothing
  113. End Sub
  114.  
  115. Sub WriteTitle(sTitle)
  116.     Response.Write "<H2>" & sTitle & "</H2>" & vbCrLf
  117. End Sub
  118.  
  119. Sub WriteTableHeader
  120.     Response.Write "<TABLE BORDER=1>" & vbCrLf
  121. End Sub
  122.  
  123. Sub WriteTableRowOpen
  124.     Response.Write "<TR>" & vbCrLf
  125. End Sub
  126.  
  127. Sub WriteTableCell(bCellIsTitle, sContents)
  128.     Response.Write vbTab & "<TD>"
  129.     If bCellIsTitle Then Response.Write "<B>"
  130.     Response.Write sContents
  131.     If bCellIsTitle Then Response.Write "</B>"
  132.     Response.Write "</TD>" & vbCrLf
  133. End Sub
  134.  
  135. Sub WriteTableRowClose
  136.     Response.Write "</TR>" & vbCrLf
  137. End Sub
  138.  
  139. Sub WriteTableFooter
  140.     Response.Write "</TABLE>" & vbCrLf
  141. End Sub
  142. ' END SUBS & FUNCTIONS SECTION
  143.  
  144.  
  145. ' BEGIN RUNTIME CODE
  146. '  Before I start with the run-time code, let me clear up a few things.
  147. '  I've tried (and succeeded I think!) to keep all the actual HTML
  148. '  formatting contained within Subs.  Hence things should be relatively
  149. '  consistent as well as being easy to change if you say want a larger
  150. '  border or perhaps a table background color or whatever...
  151. '  This, along with my attempts to try and keep my sanity, have resulted
  152. '  in a rather large proportion of Sub/Function Calls to actual code.
  153. '  Since I'm sure this is probably confusing to many newcomers to ASP
  154. '  and/or VB, I've attempted to preface each call with the optional
  155. '  Call command.  Also any SUB or FUNCTION whose name starts with the
  156. '  word "Write" is basically just an encapsulation of some variation of
  157. '  a Response.Write command, while the remainder of the name represents
  158. '  whatever it happens to write.
  159. '     IE. WriteTableRowClose writes the tags used to end (or close) a table row
  160. '  The actual HTML is (as usual) pretty vanilla flavored.  If you want
  161. '  rocky-road or mint ting-a-ling (a marvelous piece of ice cream
  162. '  craftsmanship I might add), you'll need to edit the Write functions.
  163. '  Just be aware of the fact that any change to a SUB will affect ALL
  164. '  uses of it, so check the code before you try and make a change to
  165. '  just one cell and end up changing them all!
  166. '
  167. '  Okay enough of my rambling......Onwards to the Code!!!
  168.  
  169. Dim objDC, objRS ' DataConnection and RecordSet
  170. Dim I ' As Integer - Standard Looping Var
  171. Dim strTemp ' As String - Temporary area for building long strings
  172.  
  173. Dim sAction ' As String - Action String to choose what to do
  174. Dim sTableName ' As String - ...so we know what to do it to
  175. Dim sSortField ' As String - Field to sort by
  176. Dim sSortOrder ' As String - ...ASC or DESC
  177. Dim sCritField ' As String - Field for DrillDown
  178. Dim sCritValue ' As String - ...Value to compare to
  179. Dim iCritDataType ' As Integer - so we know how to compare
  180. ' Note to all you programmers out there!
  181. ' IE4 broke this code when my QueryString was named parameter because
  182. ' it was converting the ¶ to the Paragraph sign even though it was
  183. ' in the middle of a word and there was no trailing ;.  It works great
  184. ' in Netscape.  Here's another case where IE's efforts to make things
  185. ' foolproof ruined the asp code!
  186.  
  187. ' Get all the parameters we'll need
  188. sAction = Request.QueryString("action")
  189. If sAction = "" Then sAction = "ListTables"
  190.  
  191. sTableName = Request.QueryString("tablename")
  192.  
  193. sSortField = Request.QueryString("sf")
  194. Select Case LCase(Request.QueryString("so"))
  195.     Case "asc"
  196.         sSortOrder = "ASC"
  197.     Case "desc"
  198.         sSortOrder = "DESC"
  199.     Case Else
  200.         sSortOrder = "none"
  201. End Select
  202.  
  203. sCritField = Request.QueryString("cf")
  204. If Len(sCritField) = 0 Then sCritField = ""
  205. sCritValue = Request.QueryString("cv")
  206. iCritDataType = Request.QueryString("cdt")
  207. If Len(iCritDataType) <> 0 And IsNumeric(iCritDataType) Then iCritDataType = CInt(iCritDataType)
  208.  
  209. ' Start the actual DB work
  210.  
  211. ' Code common to all choices.
  212. Call OpenConnection
  213. Call OpenRecordset(sAction)
  214.  
  215. Select Case sAction
  216.     Case "ShowDataConnectionProperties" ' Cool to look at but not really part of the sample!
  217.         ' Fake it out so we don't have problems closing the DB
  218.         OpenRecordset("ListTables")
  219.         ' Get all the DataConn Properties
  220.         For I = 0 to objDC.Properties.Count - 1
  221.             Response.Write I & " " & objDC.Properties(i).Name & ": " & objDC.Properties(I) & "<BR>" & vbCrLf
  222.         Next 'I
  223.     Case "ListTables"
  224.         Call WriteTitle("Tables")
  225.  
  226.         If Not objRS.EOF Then objRS.MoveFirst
  227.         Call WriteTableHeader
  228.  
  229.         Call WriteTableRowOpen
  230.         Call WriteTableCell(True, "Table Name")
  231.         Call WriteTableRowClose
  232.  
  233.         Do While Not objRS.EOF
  234.             ' Rule out everything but tables and don't list system tables.
  235.             ' Note MSys is only for OLEDB.
  236.             If objRS.Fields("TABLE_TYPE") = "TABLE" AND Left(objRS.Fields("TABLE_NAME"), 4) <> "MSys" Then
  237.                 Call WriteTableRowOpen
  238.                 Call WriteTableCell(False, "<A HREF=""./db_dsn.asp?action=ViewTable&tablename=" & Server.URLEncode(objRS.Fields("TABLE_NAME")) & """>" & objRS.Fields("TABLE_NAME") & "</A>")
  239.                 Call WriteTableRowClose
  240.             End If
  241.             objRS.MoveNext
  242.         Loop
  243.         Call WriteTableFooter
  244.     Case "ViewTable", "DrillDown" ' The same here but in the OpenRecordset SUB they're very different.
  245.         Call WriteTitle(sTableName)
  246.  
  247.         If Not objRS.EOF Then objRS.MoveFirst
  248.         Call WriteTableHeader
  249.         
  250.         Call WriteTableRowOpen
  251.         For I = 0 to objRS.Fields.Count - 1
  252.             ' Build heading - the "sort by" links
  253.             ' Was all on the line WriteTableCell line but I split it up for readability
  254.                 ' Field name for the heading
  255.                 strTemp = objRS.Fields(I).Name
  256.                 ' Begin Anchor for the + Sign
  257.                 strTemp = strTemp & " (<A HREF=""./db_dsn.asp"
  258.                 ' Set action
  259.                 strTemp = strTemp & "?action=DrillDown"
  260.                 ' Set table name to current table
  261.                 strTemp = strTemp & "&tablename=" & Server.URLEncode(sTableName)
  262.                 ' Set criteria field to whatever it currently is
  263.                 strTemp = strTemp & "&cf=" & Server.URLEncode(sCritField)
  264.                 ' Set criteria value to whatever it currently is
  265.                 strTemp = strTemp & "&cv=" & Server.URLEncode(sCritValue)
  266.                 ' Set criteria data type to this fields' data type
  267.                 strTemp = strTemp & "&cdt=" & iCritDataType
  268.                 ' Set sort field to this field
  269.                 strTemp = strTemp & "&sf=" & Server.URLEncode(objRS.Fields(I).Name)
  270.                 ' Set sort order to this ascending (hence the +)
  271.                 strTemp = strTemp & "&so=asc"">+</A>"
  272.                 ' End Anchor for the + Sign
  273.                 
  274.                 ' Begin Anchor for the - Sign
  275.                 ' Next 8 lines are basically the same as above except for the sort order (so)
  276.                 strTemp = strTemp & "/<A HREF=""./db_dsn.asp"
  277.                 strTemp = strTemp & "?action=DrillDown"
  278.                 strTemp = strTemp & "&tablename=" & Server.URLEncode(sTableName)
  279.                 strTemp = strTemp & "&cf=" & Server.URLEncode(sCritField)
  280.                 strTemp = strTemp & "&cv=" & Server.URLEncode(sCritValue)
  281.                 strTemp = strTemp & "&cdt=" & iCritDataType
  282.                 strTemp = strTemp & "&sf=" & Server.URLEncode(objRS.Fields(I).Name)
  283.                 strTemp = strTemp & "&so=desc"">-</A>)"
  284.                 ' End Anchor for the - Sign
  285.             
  286.             Call WriteTableCell(True, strTemp)
  287.         Next 'I
  288.         Call WriteTableRowClose
  289.  
  290.         Do While Not objRS.EOF
  291.             Call WriteTableRowOpen
  292.             For I = 0 to objRS.Fields.Count - 1
  293.                 If IsNull(objRS.Fields(I).Value) Or objRS.Fields(I).Value = "" Or VarType(objRS.Fields(I).Value)= vbNull Then
  294.                     strTemp = " "
  295.                 Else
  296.                     ' These set the drill down values which get passed if you click on any value
  297.                     strTemp = "<A HREF=""./db_dsn.asp"
  298.                     strTemp = strTemp & "?action=DrillDown"
  299.                     strTemp = strTemp & "&tablename=" & Server.URLEncode(sTableName)
  300.                     strTemp = strTemp & "&cf=" & Server.URLEncode(objRS.Fields(I).Name)
  301.                     strTemp = strTemp & "&cv=" & Server.URLEncode(objRS.Fields(I).Value)
  302.                     strTemp = strTemp & "&cdt=" & objRS.Fields(I).Type
  303.                     strTemp = strTemp & "&sf=" & Server.URLEncode(sSortField)
  304.                     strTemp = strTemp & "&so=" & sSortOrder & """>"
  305.                     strTemp = strTemp & objRS.Fields(I).Value
  306.                     strTemp = strTemp & "</A>"
  307.                 End If
  308.                 Call WriteTableCell(False, strTemp)
  309.             Next 'I
  310.             Call WriteTableRowClose
  311.             objRS.MoveNext
  312.         Loop
  313.         Call WriteTableFooter
  314. End Select
  315.  
  316. ' Close Data Access Objects and free DB variables
  317. Call CloseRecordset
  318. Call CloseConnection
  319. ' END RUNTIME CODE
  320. %>
  321. <BR>
  322. <A HREF="./db_dsn.asp">Back to the Table List</A>
  323.