ASP get database table name, field name
Take SQL Server as an example:
Copy Code code as follows:
<%
SET conn=server.createobject ("ADODB. Connection ")
Conn.Open "SERVER=IP address; provider=sqloledb;database= library name; uid= user name; pwd= password; "
%>
Read the table name in the SQL Server library:
Copy 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
%>
So, we know the table name, so let's take a look at how to manipulate the table's fields.
Suppose: There are tables in the database: [Things], fields in the table: Id,thingsname,thingstype
Get all field names for this table:
Copy 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 ("First" & i+1 & "field Name:" & Rs. Fields (i). Name & "<br/><br/>")
Next
%>
OK, now we know how to get the name of the field.
If you want to do something about the value of the field you get, this is OK:
For example, if we want to delete the field thingstype in the table [things], we can
To write in this way:
Copy Code code as follows:
<%
Sql= "ALTER TABLE [things] DROP COLUMN thingstype"
Conn.execute SQL
%>
For example, we want to add a field thingscolor, its type is varchar type, length is 20, and the default value is red, as follows:
Copy Code code as follows:
<%
Sql= "ALTER TABLE [things] ADD thingscolor VARCHAR () DEFAULT ' Red '
Conn.execute SQL
%>
The basic operation of the above fields is implemented in the SQL language, in the ASP, through the SQL language, we have sufficient permissions to complete
More database operations, such as create tables, delete tables with drop, and so on.