读取ACCESS库中所有表及其字段名称
公司有一套ASP+ACCESS的后台程序,我要尽快了解,在了解其数据库结构的时候对着ACCESS非常的不直观,便想将所有表名称及其字段名称打印出来,输出这些内容便成问题,有没有办法用ASP将这些内容输出到一个页面呢?搜索了相关资料后总算是实现了,下边是程序:
其中主要是运用了Connection对象的openschema方法,这边要注意的是rs("table_type")="TABLE"一句中TABLE一定要大写。此方法在ASP教材中很少看到,搜索了大量资料后我还不是非常了解,下边是我搜索到的两篇相关文章,有兴趣的朋友可以看看:
1.http://dev.csdn.net/article/68/68466.shtm
2.http://fishcat.blog.com.cn/archives/2006/386828.shtml
1
<%
2
Dim conn,rs,rs2,sqlstr,t_count,table_name,i
3
On error resume next
4
Set conn = Server.CreateObject("ADODB.Connection")
5
Conn.Connectionstring="provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath("data1.mdb")
6
Conn.open
7
If err then
8
Response.write "请检查数据库路径!"
9
err.clear
10
Response.End
11
End if
12
T_count = 0
13
Set rs = Conn.openschema(20)
14
Do while not rs.eof
15
If rs("table_type")="TABLE" then
16
T_count = T_count + 1
17
table_name = rs("table_name")
18
Response.write "<b>表" & table_name & ":</b><br>"
19
Sqlstr = "select * from " & table_name
20
Set rs2 = Server.CreateObject("ADODB.RecordSet")
21
Rs2.open sqlstr,conn,0,1
22
For i=0 to rs2.fields.count-1
23
If i = rs2.fields.count-1 then
24
Response.write rs2.fields.item(i).name
25
Else
26
Response.write rs2.fields.item(i).name & ","
27
End if
28
Next
29
Response.write "<p>"
30
Rs2.close
31
Set rs2 = nothing
32
End if
33
Rs.movenext
34
Loop
35
Response.write "<u><b>总计</b><font color=red>" & t_count & "</font><b>个表</b></u>"
36
Rs.close
37
Set rs = nothing
38
Conn.close
39
Set conn=nothing
40
%>
<%2
Dim conn,rs,rs2,sqlstr,t_count,table_name,i3
On error resume next4
Set conn = Server.CreateObject("ADODB.Connection")5
Conn.Connectionstring="provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath("data1.mdb")6
Conn.open7
If err then8
Response.write "请检查数据库路径!"9
err.clear10
Response.End11
End if12
T_count = 013
Set rs = Conn.openschema(20)14
Do while not rs.eof15
If rs("table_type")="TABLE" then16
T_count = T_count + 117
table_name = rs("table_name")18
Response.write "<b>表" & table_name & ":</b><br>"19
Sqlstr = "select * from " & table_name20
Set rs2 = Server.CreateObject("ADODB.RecordSet")21
Rs2.open sqlstr,conn,0,122
For i=0 to rs2.fields.count-123
If i = rs2.fields.count-1 then24
Response.write rs2.fields.item(i).name25
Else26
Response.write rs2.fields.item(i).name & ","27
End if28
Next29
Response.write "<p>"30
Rs2.close31
Set rs2 = nothing32
End if33
Rs.movenext34
Loop35
Response.write "<u><b>总计</b><font color=red>" & t_count & "</font><b>个表</b></u>"36
Rs.close37
Set rs = nothing38
Conn.close39
Set conn=nothing40
%>其中主要是运用了Connection对象的openschema方法,这边要注意的是rs("table_type")="TABLE"一句中TABLE一定要大写。此方法在ASP教材中很少看到,搜索了大量资料后我还不是非常了解,下边是我搜索到的两篇相关文章,有兴趣的朋友可以看看:
1.http://dev.csdn.net/article/68/68466.shtm
2.http://fishcat.blog.com.cn/archives/2006/386828.shtml
------------------------------------------------
dnawo:天才在于勤奋,知识在于积累!

浙公网安备 33010602011771号