Use Database statements
1. SELECT statement: the Command database engine returns information from the database as a group of records.
2. insert into statement: add one or more records to a table.
3. Update statement: Create an update query to change the field values in a specified table based on specific rules.
4. Delete statement: Create a delete query to clear one or more tables that list records from the from clause and comply with the WHERE clause.
5. Execute statement: Used to activate procedure (process)
Use ASP to create your own address book trainer ......
1. Create a database:
Use Microsoft Access to create an empty database named data. MDB and use the designer to create a new table. Enter the following fields:
Field name data type description others
Id automatic number data Id field size: Long Integer new value: Incremental index: Yes (no duplicates)
Username text name Default Value
Usermail text E-mail Default Value
View number viewing times field size: Long Integer default value: 0 index: None
Default Value of indate time and date addition time
Saved as a data. MDB file. For ease of description, a simple library is provided.
2. Connect to the database
Party * 1:
Set conn = server. Createobject ("ADODB. Connection"
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & server. mappath ("data. mdb"
Party * 2:
Set conn = server. Createobject ("ADODB. Connection"
Conn. Open "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("data. mdb ":'(
Note: On a page, you only need to connect once. After the database is used up, close the connection in time.
Conn. Close
Set conn = nothing
3. Add a new record to the database
Set conn = server. Createobject ("ADODB. Connection ":'(
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & server. mappath ("data. mdb ":'(
Username = "sudden changes"
Usermail = "fytb@163.com"
Indate = now ()
SQL = "insert into data (username, usermail, indata) values ('" & username & "', '" & usermail & "', '" & indate &"')"
Conn.exe cute (SQL)
Conn. Close
Set conn = nothing
Note: Create a database connection. Get the name and E-mail string through the form, and now () to get the current time and date. Use the insert into syntax new record to execute the statement conn.exe cute. Close the statement.
4. Select records in the database
1. Select fields of all records (sorted in reverse order of records): SQL = "select * from data order by ID DESC"
2. Select the names and e-mail fields of all records (unordered): SQL = "select username, usermail from data"
3. Select All records whose names are "sudden changes": SQL = "select * from data where username = '" sudden changes "'"
4. Select All records using the 163 mailbox (sorted by the number of views): SQL = "select * from data where usermail like '%" @ 163.com" % 'order by view DESC"
5. Select the latest 10 records: SQL = "select top 10 * from data order by ID DESC"
The SQL statement is already known. However, when using a web application, you must create a recordset object to obtain the record set to apply the value retrieved from the database to the webpage, if all the records are displayed on the web page:
Set conn = server. Createobject ("ADODB. Connection ":'(
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & server. mappath ("data. mdb ":'(
SQL = "select * from data"
Set rs = server. Createobject ("ADODB. recordset ":'(
Rs. Open SQL, Conn, 1, 1
Do while not Rs. EOF
Response. write "<p> name:" & RS ("username": '(& "E-mail:" & RS ("usermail") & "View: "& RS (" View ") &" times "& RS (" indate ") &" Join </P>"
Rs. movenext
Loop
Rs. Close
Set rs = nothing
Conn. Close
Set conn = nothing
Note: Create a database connection. Create rs to obtain the record set. Record is displayed cyclically. Rs. EOF indicates the end of the record, Rs. movenext indicates moving to the next record, and close the record.
5. Modify (update) database records
Modify the record's email:
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & server. mappath ("data. mdb ")
Id = 1
Usermail = "fytb@163.com"
SQL = "Update Data Set usermail = '" & usermail & "'where id =" & CINT (ID)
Conn.exe cute (SQL)
Conn. Close
Set conn = nothing
Note: Create a database connection; obtain the record ID and new e-mail string; Use the update sentence to modify the record; execute the record ;conn.exe cute; and close it.
If you add 1 to the record view value, SQL = "update data set view = view + 1 where id =" & CINT (ID)
6. delete database records
Delete a record:
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & server. mappath ("data. mdb ")
Id = 1
SQL = "delete from data where id =" & CINT (ID)
Conn.exe cute (SQL)
Conn. Close
Set conn = nothing
Note: Create a database connection to obtain the record ID. Use the delete sentence to delete the record and execute the statement conn.exe cute.
Delete multiple records as follows: SQL = "delete from data where ID in (id1, Id2, ID3 )"
Delete all records as follows: SQL = "delete from data"