Database access - table creation


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  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. ' This line is for the Access sample database:
  17. 'cnnSimple.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
  18. ' We're actually using SQL Server so we use this line instead:
  19. cnnSimple.Open "Provider=SQLOLEDB;Data Source=10.2.1.214;" _
  20. & "Initial Catalog=samples;User Id=samples;Password=password;" _
  21. & "Connect Timeout=15;Network Library=dbmssocn;"
  22. ' Execute a query using the connection object. It automatically
  23. ' creates and returns a recordset which we store in our variable.
  24. Set rstSimple = cnnSimple.Execute("SELECT * FROM scratch")
  25. ' Display a table of the data in the recordset. We loop through the
  26. ' recordset displaying the fields from the table and using MoveNext
  27. ' to increment to the next record. We stop when we reach EOF.
  28. %>
  29. <table border="1">
  30. <%
  31. Do While Not rstSimple.EOF
  32. %>
  33. <tr>
  34. <td><%= rstSimple.Fields("id").Value %></td>
  35. <td><%= rstSimple.Fields("text_field").Value %></td>
  36. <td><%= rstSimple.Fields("integer_field").Value %></td>
  37. <td><%= rstSimple.Fields("date_time_field").Value %></td>
  38. </tr>
  39. <%
  40. rstSimple.MoveNext
  41. Loop
  42. %>
  43. <%
  44. ' Close our recordset and connection and dispose of the objects
  45. rstSimple.Close
  46. Set rstSimple = Nothing
  47. cnnSimple.Close
  48. Set cnnSimple = Nothing
  49. ' That's all folks!
  50. %>
  51.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.