ASP and database applications (for beginners) [Author: Anonymous Copy from: Site author Hits: 1449 Update Time: 2004-4-6 article entry: ywdong]
Seeing that many netizens asked some basic questions about database operations, now I have written a simple tutorial to write, so that some beginners start: ASP and database applications (for beginners) Generally, one Really, a complete site is inseparable from the database, because in practical applications, there are many more data that requires saving, and this data often has associations, and the database is used to manage this data, it can be easily queried and updated. There are many species, such as: Fox Database (.dbf), Access Database (.MDB), Informix, Oracle, and SQL Server, etc., here I will explain how the ASP accesses the database as an example with the Microsoft Access database. Common database statement 1.Select statement: The command database engine returns information from the database as a set of records. 2. INSERT INTO statement: Add one or more records to a table. 3.Update statement: Create an update query to change field values in the specified table based on a specific criterion. 4. DELETE statement: Create a delete query to list the record from the From clause and comply with one or more of the WHERE clause. 5.EXECUTE statement: Used to activate the procedure to use ASP to make a self-cultivation test, build a database: build a Data.mdb with Microsoft Access, create a DATA.MDB, create a designer New table. Enter the following fields: Field Name Data Type Description Other ID Auto Number Data Identifier Field Size: Long Integer Novel Value: Increment: (None Den) UserName Text Name Default Usermail Text E-Mail Default VIEW Digital View a number of digital segments: Long integer default: 0 Index: No Indate Time Date Added Time Default Value Save as Data.mdb file, in order to facilitate explanation, just a relatively simple library. Second, connection database method 1: set conn = server.createObject ("adodb.connection") conn.open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.MAppath ("DATA.MDB") Method 2: Set conn = server.createObject ("adoDb.connection") conn.d "provider = microsoft.jet.oledb.4.0; data source =" & Server.mappath ("data.mdb") Note: A page, As long as the connection is connected, it is possible to turn off the connection in time after the database is used.
Conn.close set conn = nothing 3, add new record to database set conn = server.createObject ("adodb.connection") conn.open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.mappath ("DATA.MDB") UserName = "Storm mutation" usermail = "fytb@163.com" Indate = now () SQL = "INTO DATA (UserName, Usermail, Indata) VALUES ('" & username & ",'" & usermail & "','" & indecute (sql) conn.close set conn = Nothing Description: Create a database connection; get the name, e-mail string, now () get the current time date; use The INSERT INTO statement adds a new record; conn.execute is executed; finally closed.
Fourth, select the record in the database 1. Select all recorded fields (sorted by recording step): SQL = "Select * from data order by id" 2. Select all recorded names and E-mail fields (not sorted): SQL = "SELECT Username, Usermail from Data" 3. Select all records named "Square": SQL = "Select * from data where username = '" "Wind cloud mutation" "4. Choose all records using 163 mailbox ( Sort by viewing): SQL = "Select * from data where usermail like '%" @ 163.com "%' Order by View Desc" 5. Choose the latest 10 records: SQL = "SELECT TOP 10 * from data Order BY ID DESC "SQL statement already knows, but when the web application, you have to create a RecordSet object to get the record set, to apply the value taken from the database to the web page, if now put all the records on the web page This way: set conn = server.createObject ("adoDb.connection") conn.open "Driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.mappath ("DATA.MDB") SQL = "SELECT * From data "set = server.createObject (" adodb.recordset ") RS.Open SQL, CONN, 1, 1 do while not rs.eof response.write"
Name: "& r (" UserName ") & "E-mail:" & RS ("Usermail") & "View:" & RS ("VIEW") & "Supreme" & RS ("INDATE") & "Join P>" RS.MOVENEXT LOOP RS. Close Set RS = Nothing Conn.close Set Conn = Nothing Description: Create a database connection; create RS to get record set; loop display record, rs.eof represents the end of the record, rs.move Next indicates to the next record; finally closed.