This article mainly introduces the method for ASP to obtain database table names, library names, and field names. The example in this article uses the SqlServer database. Friends who need it can refer to it.
ASP obtains database table name and field name
Take SQLServer as an example:
Copy the code code as follows:<%
SET Conn=Server.CreateObject(ADODB.Connection)
Conn.Open Server=IP address;Provider=sqloledb;Database=library name;UID=username;PWD=password;
%>
Read the table name in the SqlServer library:
Copy the code code as follows:<%
Set rs=Conn.OpenSchema(20)
While not rs.EOF
Response.Write(database name: & rs(0) & <br>)
Response.Write(owner: & rs(1) & <br />)
Response.Write(table name: & rs(2) & <br />)
Response.Write(Table type: & rs(3) & <br />)
rs.MoveNext
Wend
%>
In this way, we know the table name, now let's take a look at how to operate the fields of the table.
Assumption: There is a table in the database: [Things], and the fields in the table are: id, thingsName, thingsType
Get all field names of the table:
Copy the code code as follows:<%
Dim i,j,Sql
Set rs=Server.CreateObject(ADODB.Recordset)
Sql=select * from [Things] where 1<>1
rs.open sql,Conn,1,1
j=rs.Fields.count
For i=0 to (j-1)
Response.Write(& i+1 & field name: & rs.Fields(i).Name & <br /><br />)
Next
%>
Okay, now we understand how to get the field name.
If you want to perform some operations on the obtained field values, this is also possible:
For example, if we want to delete the field thingsType in the table [Things], we can
Write it like this:
Copy the code code as follows:
<%
Sql=ALTER TABLE [Things] DROP COLUMN thingsType
Conn.execute Sql
%>
For another example, we want to add a field thingsCOLOR. Its type is varchar, the length is 20, and the default value is Red. It is written as follows:
Copy the code code as follows:
<%
Sql=ALTER TABLE [Things] ADD thingsCOLOR VARCHAR(20) DEFAULT 'Red'
Conn.execute Sql
%>
The above basic operations on fields are all implemented in SQL language. In ASP, through SQL language, we can complete it as long as we have sufficient permissions.
More database operations, such as using CREATE to create tables, using DROP to delete tables, etc.