利用OleDb的GetOLEDBSchemaTable方法得到数据库架构信息
我们可以利用OleDb的GetOLEDBSchemaTable方法得到数据库的所有视图,表,存储过程等信息。
GetDataBaseSchema.aspx
GetDataBaseSchema.aspx.vb
GetDataBaseSchema.aspx
1<%@ Page Language="vb" AutoEventWireup="false" Codebehind="GetDataBaseSchema.aspx.vb"
2Inherits="aspxWeb.GetDataBaseSchema"%>
3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
4<HTML>
5<HEAD>
6<title>GetDataBaseSchama</title>
7<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
8<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
9<meta name="vs_defaultClientScript" content="JavaScript">
10<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
11</HEAD>
12<body MS_POSITIONING="GridLayout">
13<form id="Form1" method="post" runat="server">
14<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid><br>
15<asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid><br>
16<asp:DataGrid id="DataGrid3" runat="server"></asp:DataGrid><br>
17<asp:DataGrid id="DataGrid4" runat="server"></asp:DataGrid><br>
18<asp:DataGrid id="Datagrid5" runat="server"></asp:DataGrid><br>
19<asp:DataGrid id="Datagrid6" runat="server"></asp:DataGrid>
20</form>
21</body>
22</HTML>
2Inherits="aspxWeb.GetDataBaseSchema"%>
3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
4<HTML>
5<HEAD>
6<title>GetDataBaseSchama</title>
7<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
8<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
9<meta name="vs_defaultClientScript" content="JavaScript">
10<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
11</HEAD>
12<body MS_POSITIONING="GridLayout">
13<form id="Form1" method="post" runat="server">
14<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid><br>
15<asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid><br>
16<asp:DataGrid id="DataGrid3" runat="server"></asp:DataGrid><br>
17<asp:DataGrid id="DataGrid4" runat="server"></asp:DataGrid><br>
18<asp:DataGrid id="Datagrid5" runat="server"></asp:DataGrid><br>
19<asp:DataGrid id="Datagrid6" runat="server"></asp:DataGrid>
20</form>
21</body>
22</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
Web Form Designer Generated Code
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
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
Web Form Designer Generated Code
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