Posted By

ryanprel on 02/20/07


Tagged

sql ASP


Versions (?)

Database access - table creation


 / Published in: Other
 

  1. <%
  2. ' Declare our variables... always good practice!
  3. Dim cnnSimple ' ADO connection
  4. Dim rstSimple ' ADO recordset
  5. Dim strDBPath ' path to our Access database (*.mdb) file
  6.  
  7.  
  8. ' MapPath of virtual database file path to a physical path.
  9. ' If you want you could hard code a physical path here.
  10. strDBPath = Server.MapPath("db_scratch.mdb")
  11.  
  12.  
  13. ' Create an ADO Connection to connect to the scratch database.
  14. ' We're using OLE DB but you could just as easily use ODBC or a DSN.
  15. Set cnnSimple = Server.CreateObject("ADODB.Connection")
  16.  
  17. ' This line is for the Access sample database:
  18. 'cnnSimple.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
  19.  
  20. ' We're actually using SQL Server so we use this line instead:
  21. cnnSimple.Open "Provider=SQLOLEDB;Data Source=10.2.1.214;" _
  22. & "Initial Catalog=samples;User Id=samples;Password=password;" _
  23. & "Connect Timeout=15;Network Library=dbmssocn;"
  24.  
  25.  
  26. ' Execute a query using the connection object. It automatically
  27. ' creates and returns a recordset which we store in our variable.
  28. Set rstSimple = cnnSimple.Execute("SELECT * FROM scratch")
  29.  
  30.  
  31. ' Display a table of the data in the recordset. We loop through the
  32. ' recordset displaying the fields from the table and using MoveNext
  33. ' to increment to the next record. We stop when we reach EOF.
  34. %>
  35. <table border="1">
  36. <%
  37. Do While Not rstSimple.EOF
  38. %>
  39. <tr>
  40. <td><%= rstSimple.Fields("id").Value %></td>
  41. <td><%= rstSimple.Fields("text_field").Value %></td>
  42. <td><%= rstSimple.Fields("integer_field").Value %></td>
  43. <td><%= rstSimple.Fields("date_time_field").Value %></td>
  44. </tr>
  45. <%
  46. rstSimple.MoveNext
  47. Loop
  48. %>
  49. </table>
  50. <%
  51. ' Close our recordset and connection and dispose of the objects
  52. rstSimple.Close
  53. Set rstSimple = Nothing
  54. cnnSimple.Close
  55. Set cnnSimple = Nothing
  56.  
  57. ' That's all folks!
  58. %>
  59.  

Report this snippet  

You need to login to post a comment.