首页 > 开发 > 综合 > 正文

利用OleDb的GetOLEDBSchemaTable方法得到数据库架构信息

2024-07-21 02:23:06
字体:
来源:转载
供稿:网友
我们可以利用oledb的getoledbschematable方法得到数据库的所有视图,表,存储过程等信息。

getdatabaseschema.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="getdatabaseschema.aspx.vb"
inherits="aspxweb.getdatabaseschema"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>getdatabaseschama</title>
<meta name="generator" content="microsoft visual studio.net 7.0">
<meta name="code_language" content="visual basic 7.0">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<asp:datagrid id="datagrid1" runat="server"></asp:datagrid><br>
<asp:datagrid id="datagrid2" runat="server"></asp:datagrid><br>
<asp:datagrid id="datagrid3" runat="server"></asp:datagrid><br>
<asp:datagrid id="datagrid4" runat="server"></asp:datagrid><br>
<asp:datagrid id="datagrid5" runat="server"></asp:datagrid><br>
<asp:datagrid id="datagrid6" runat="server"></asp:datagrid>
</form>
</body>
</html>

getdatabaseschema.aspx.vb

imports system
imports system.data
imports system.data.oledb

public class getdatabaseschema
inherits system.web.ui.page
protected withevents datagrid2 as system.web.ui.webcontrols.datagrid
protected withevents datagrid3 as system.web.ui.webcontrols.datagrid
protected withevents datagrid4 as system.web.ui.webcontrols.datagrid
protected withevents datagrid5 as system.web.ui.webcontrols.datagrid
protected withevents datagrid6 as system.web.ui.webcontrols.datagrid
protected withevents datagrid1 as system.web.ui.webcontrols.datagrid

#region " web form designer generated code "
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

end sub

private sub page_init(byval sender as system.object, byval e as system.eventargs) _
handles mybase.init
initializecomponent()
end sub

#end region

private sub page_load(byval sender as system.object, byval e as system.eventargs) _
handles mybase.load
'如何得到数据库中的架构信息?
'getoledbschematable函数有两个方法:
'oledbschemaguid
'restrictions
'参数oledbschemaguid 的成员:tables, procedures, views, columns, catlogs 等
'参数restrictions为限制条件,是一个对象数组,原来过虑架构结果信息,
'每一个对象映射到所返回的datacolumn的值。


dim strcnn as string
strcnn = "provider=sqloledb; data source=./netsdk; initial catalog=pubs;user id=sa;password=;"
dim dataconn as new oledbconnection(strcnn)
try
dataconn.open()
dim schematable as datatable
schematable = dataconn.getoledbschematable(oledbschemaguid.tables, nothing)
'得到全部的表、视图
datagrid1.datasource = schematable
datagrid1.databind()

schematable = dataconn.getoledbschematable(oledbschemaguid.tables, new object() {nothing, nothing, nothing, "table"})
'得到全部的用户表,用户表类型为table,进行过虑
datagrid2.datasource = schematable
datagrid2.databind()

schematable = dataconn.getoledbschematable(oledbschemaguid.tables, new object() {nothing, nothing, nothing, "view"})
'得到全部的视图
datagrid3.datasource = schematable
datagrid3.databind()

schematable = dataconn.getoledbschematable(oledbschemaguid.procedures, nothing)
'得到全部的存储过程
datagrid4.datasource = schematable
datagrid4.databind()

schematable = dataconn.getoledbschematable(oledbschemaguid.provider_types, nothing)
'得到全部支持的数据类型
datagrid5.datasource = schematable
datagrid5.databind()

schematable = dataconn.getoledbschematable(oledbschemaguid.primary_keys, nothing)
datagrid6.datasource = schematable
datagrid6.databind()
catch ex as exception
response.write(ex.message.tostring())
finally
dataconn.close()
end try

end sub

end class




收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表