Access database operation add field View Field

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.