Usually we have only heard of one type of ASP objects such as ADO, but there is a little-known ASP access object specifically for SQL Server in ASP, which is the SQLOLE.SQLServer object. SQLOLE.SQLServer can directly access the system properties of SQL Server. The Set oSQLServer = CreateObject ("SQLOLE.SQLServer") statement will generate a SQL Server server object.
---- To view the collection of databases in this server, the available statement:
For Each SQLDB In oSQLServer.Databases
Response.Write SQLDB.Name 'will list all databases, such as Pubs, etc.
Next
---- To view the collection of data tables in a database (such as PUBS database), you can use the following statement:
pubDatabase=oSQLServer.Databases("pubs")
'oSQLServer is the SQL Server server object created earlier
For Each DBTable In pubDatabase.Tables
Response.WriteDBTable.Name
Next
---- The following statement will list the data views in the database (PUBS database)
pubDatabase =oSQLServer.Databases("pubs")
'oSQLServer is the SQL Server server object created earlier
For Each DBView In pubDatabase.Views
Response.Write DBView.Text
Next
---- The following statement will list the stored procedures in the database (PUBS database)
pubDatabase =oSQLServer.Databases("pubs")
'oSQLServer is the SQL Server server object created earlier
For Each DBSP In pubDatabase.StoredProcedures
Response.Write DBSP.Text
Next
---- The result of running the program (get the name of the data table):
---- Get the result of the stored process:
---- A complete small example source code is attached (readers with other functions can add it).
< %@ LANGUAGE = VBScript % >
< HTML >
<HEAD>
< META NAME="GENERATOR" Content=
"Microsoft Developer Studio" >
< META HTTP-EQUIV="Content-Type"
content="text/html; charset=gb2312" >
< TITLE >< /TITLE >
< /HEAD >
< %
On Error Resume Next
Dim oSQLServer
Set oSQLServer = CreateObject ("SQLOLE.SQLServer")
strServer = "dep"
strLogin = "sa"
strPwd = ""
oSQLServer.Connect strServer,strLogin,strPwd
%>
<BODY BGCOLOR=#ffffff>
Database list
< SELECT NAME="Database" >
<%
For Each SQLDB In oSQLServer.Databases
If Not SQLDB.SystemObject Then
Response.Write "< OPTION VALUE=
""" & SQLDB.Name & """ >" & SQLDB.Name
& " "
End If
Next
Set oSQLServer = Nothing
%>
< /SELECT>
< /BODY >
< /HTML>