ASP database operation teaching code (original)

xiaoxiao2021-03-06  100

First of all, IIS has been installed, there is a problem.

Read and write the database code:

<%

'Establish Connection and RecordSet objects

SET CNN = Server.createObject ("AdoDb.Connection")

SET RS = Server.createObject ("AdoDb.Recordset")

'Open the database

CNN.Open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & server.mappath ("ACCESS file name under the current page")

'Note:

'Connecting the SQL Server database as

'Cnn.open "driver = {SQL Server}; server = database server address; UID = user name; PWD = password; DateBase = default database"

'Connect ODBC, if the username or password is empty, directly use two consecutive dual quotes ""

'Cnn.open "ODBC DSN Name", "User Name", "Password"

'Open data sheet

SQL = "SELECT * FROM table name"

The meaning of '1 and 3 you should know that if you don't know all the circumstances, use these two parameters.

RS.Open SQL, CNN, 1, 3

'Determine if the database is empty

IF r.eof then

The response.write "database is empty."

END IF

'Add a blank line in the data sheet

Rs.addnew

'Write data for empty lines

RS ("Name") = "yangyang"

RS ("pass") = "yangyang"

'Don't forget to update

Rs.Update

'Modify the record, and add a record similar to, just there is no RS.AddNew

'Delete the current line in the data table

Rs.delete

'You can also use the SQL statement to operate the database, assume that the data table is called Users, there are both fields of User, Pass, Time.

'Insert record with SQL

Struse = "yang"

Strpass = "zhao"

Strtime = now () 'Note: Current time

SQL = "INSERT INTO USERS (User, Name, Time) VALUES ('" & struser & ",'" & strpass & ", '" & setime & ")"

Cnn.execute (SQL)

'Delete records with SQL

SQL = "delete user = '" & struser & "'"

Cnn.execute (SQL)

'Modify record with SQL

SQL = "Update users set pass = 'abcd' where user = 'yang'"

Cnn.execute (SQL)

'Print all data on the web page

DO Until (RS.eof)

'Key can be the sequence number of the field (starting from 1), or the field name, the field name should be caused by double quotes. Response.write RS (Key1) & RS (Key2)

Rs.next

Loop

'Close the table object and connection object

Rs.close

SET RS = Nothing

CNN.Close

SET CNN = Nothing

%>

转载请注明原文地址:https://www.9cbs.com/read-124479.html

New Post(0)