Access
<%
'Function: displays the table name, field name, and field content in the database.
'Original: wangsdong
'Source: aspprogram.cn
'The Original article, reprinted please keep this information, thank you
Set rs = server. Createobject ("ADODB. recordset ")
DB = "db1.mdb"
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath (db)
Conn. Open connstr
Set rs = conn. openschema (20)
Do until Rs. EOF
If RS (3) = "table" then
Response. Write "table name:" & RS (2) & "<br/>"
Set RS1 = server. Createobject ("ADODB. recordset ")
SQL = "select * from" & RS (2)
Set rs1=conn.exe cute (SQL)
Response. Write "field name :"
For I = 0 to rs1.fields. Count-1
Response. Write rs1.fields (I). Name &""
Next
Response. Write "<br/>"
Do while not rs1.eof
Response. Write ""
For I = 0 to rs1.fields. Count-1
T = rs1.fields (I). Name
Response. Write RS1 (t )&""
Next
Response. Write ""
Rs1.movenext
Loop
Response. Write ""
End if
Rs. movenext
Loop
Set rs = nothing
Set conn = nothing
%>
SQL Server
<%
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "Server = IP address; provider = sqloledb; database = database name; uid = user name; Pwd = password ;"
%>
Name of the table in the read SQL Server database:
<%
Set rs = conn. openschema (20)
While not Rs. EOF
Response. Write ("Database Name:" & RS (0 )&"
")
Response. Write ("Owner:" & RS (1 )&"
")
Response. Write ("table name:" & RS (2 )&"
")
Response. Write ("Table type:" & RS (3 )&"
")
Rs. movenext
Wend
%>
Now that we know the table name, let's take a look at how to operate the table fields.
Assume that the database contains the table [things]. The table fields are ID, thingsname, and thingstype.
Obtain the names of all fields in the table:
<%
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 ("no." & I + 1 & "field name:" & Rs. Fields (I). Name &"
")
Next
%>
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 you want to delete the thingstype field in the table [things], you can
Write as follows:
<%
SQL = "ALTER TABLE [things] Drop column thingstype"
Conn.exe cute SQL
%>
For example, if we want to add a field thingscolor of the varchar type with a length of 20 and a default value of red, the statement is as follows:
<%
SQL = "ALTER TABLE [things] add thingscolor varchar (20) default Red"
Conn.exe cute SQL
%>
The above basic operations on fields are implemented in the SQL language. In ASP, with the SQL language, we can do this with sufficient permissions.
More database operations, such as using create to create tables, and using drop to delete tables