-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessDB.asp
173 lines (135 loc) · 4 KB
/
AccessDB.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<%@ Language=VBScript %>
<% Option Explicit %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
' this set of scripts demonstrates how to open a Microsoft Access
' .mdb file and display the contents of a given table.
On Error Resume Next
' Constants
const adOpenForwardOnly = 0 ' Forward only movement through records
const adOpenKeyset = 1 ' Movement any direction
const adOpenDynamic = 2
const adOpenStatic = 3
const adLockOptimistic = 3
' CheckError: Checks for, and reports Errors.
' Returns True if an error has occured, False otherwise
Function CheckError()
if Err.number <> 0 then
Response.Write "<p><FONT color=red><I>A run-time error occurred.<BR>Error Number: " &_
Err.number & "<BR>Error Description: " & Err.description & "</Font></I>"
CheckError = True
Else
CheckError = False
End If
end Function
' OpenDatabase: Opens a database connection
' IN:
' - DatabaseVirtualFilename: Virtual path to database file (.mdb)
' - Username: Username
' - Password: Password
' OUT:
' - Connection: The database connection
' RETURNS:
' - True if successful, False otherwise
Function OpenDatabase(DatabaseVirtualFilename, Username, Password, byref Connection)
' Initialise
OpenDatabase = False
Dim DatabaseFilename
DatabaseFilename = Server.MapPath(DatabaseVirtualFilename)
Dim ConnectionString
ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};" &_
"DBQ=" & DatabaseFilename & ";DefaultDir=;" &_
"UID=" & Username & ";" &_
"PWD=" & Password & ";"
Set Connection = Server.CreateObject("ADODB.Connection")
Connection.ConnectionTimeout = 30
Connection.CommandTimeout = 80
Connection.Open ConnectionString
OpenDatabase = True
end Function
' DisplayTable: Displays the contents of a table
' IN:
' - Connection: Connection to a database
' - TableName: The table to list
Sub DisplayTable(Connection, TableName)
Response.Write "<h2>Contents of table: " & TableName & "</h2>" & vbCRLF
Dim SQL
SQL = "SELECT * FROM " & TableName
' Create a RecordSet
Dim rs
set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open SQL, Connection, adOpenForwardOnly, adLockOptimistic
if rs.EOF or rs.BOF Then
Response.Write "<p align=center><i> -- No records --</i>" & vbCRLF
rs.Close
set rs = nothing
Exit sub
end if
' Start the table
Response.Write "<table width='100%' border=1>" & vbCRLF
' Write field headings
Response.Write "<tr>"
Dim item
For each item in rs.Fields
Response.Write "<td valign=top><b>" & item.Name & "</b></td>"
next
Response.Write "</tr>" & vbCRLF
' Write out records
while not rs.EOF
Response.Write "<tr>"
For each item in rs.Fields
Response.Write "<td valign=top>" & item.Value & "</td>"
next
Response.Write "</tr>" & vbCRLF
rs.MoveNext
wend
' End the table
Response.Write "</table>" & vbCRLF
rs.Close
set rs = nothing
End Sub
' ////////////////////////////////////////////////////////////
' Main
' Get the database and table names. If non-null then display the
' details, otherwise just display the input form.
Dim databasefile, databasetable
databasefile = Request("database")
databasetable = Request("table")
if (databasefile <> "" and databasetable <> "") then
Dim Connection
if (OpenDatabase(databasefile, "", "", Connection)) Then
DisplayTable Connection, databasetable
End if
Connection.Close
set Connection = nothing
CheckError
End If
%>
<br>
<br>
<!--
This form is for specifying the database and table names. The
form posts the data back to this file
-->
<form action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<table>
<tr>
<td nowrap>Database filename (virtual path):</td>
<td><INPUT type="text" size=40 name=database value="<%=databasefile%>"></td>
</tr>
<tr>
<td nowrap>Database Table:</td>
<td><INPUT type="text" size=40 name=table value="<%=databasetable%>"></td>
</tr>
<tr>
<td nowrap></td>
<td><INPUT type="submit" value=" Go! "></td>
</tr>
</table>
</form>
</BODY>
</HTML>