csdn许多网友问怎样列出数据库有所有表及表信息,下面这段程序正是你所想要的,
程序思想:用select name from sysobjects where xtype = 'u'得到所有表,然后循环打开表,根据rs_colums.fields(i).name 得到字段名,fieldtype(rs_colums.fields(i).type) 得到字段类型,rs_colums.fields(i).definedsize '宽度
由于rs_colums.fields(i).type返回类型是数字,程序中写了一个fieldtype函数转化成中文类型
private sub command1_click()
dim cn as new adodb.connection
dim rs_table as new adodb.recordset
dim rs_colums as new adodb.recordset
with cn '定义连接
.cursorlocation = aduseclient
.provider = "sqloledb"
.properties("data source").value = "lihg"
.properties("initial catalog").value = "northwind"
.properties("user id") = "sa"
.properties("password") = "sa"
.properties("prompt") = adpromptnever
.connectiontimeout = 15
.open
if .state = adstateopen then
rs_table.cursorlocation = aduseclient '得到所有表名
rs_table.open "select name from sysobjects where xtype = 'u'", cn, adopendynamic, adlockreadonly
rs_table.movefirst
do while not rs_table.eof
debug.print rs_table.fields("name")
rs_colums.cursorlocation = aduseclient
rs_colums.open "select top 1 * from [" & rs_table.fields("name") & "]", cn, adopenstatic, adlockreadonly
for i = 0 to rs_colums.fields.count - 1 ' 循环所有列
debug.print rs_colums.fields(i).name '字段名
debug.print fieldtype(rs_colums.fields(i).type) '字段类型
debug.print rs_colums.fields(i).definedsize '宽度
next
rs_colums.close
rs_table.movenext
loop
rs_table.close
set rs_colums = nothing
set rs_table = nothing
else
msgbox "数据库连接失败,请找系统管理员进行检查 !", 16, cprogramname
end
end if
end with
end sub
'*********************************************************
'* 名称:fieldtype
'* 功能:返回字段类型
'* 用法:fieldtype(ntype as integer)
'*********************************************************
function fieldtype(ntype as integer) as string
select case ntype
case 128
fieldtype = "binary"
case 11
fieldtype = "bit"
case 129
fieldtype = "char"
case 135
fieldtype = "datetime"
case 131