Using SQL statements to manipulate databases in ASP

Source: Internet
Author: User
Tags dsn
Data | database | statement

When using ASP to manipulate the database, many people use the recordset, but I feel better with the SQL statement, so I wrote this article, I hope it will help you.

<%
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The following paragraphs are connected to the database and disconnected (the database is in Access format and is connected by DSN)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Connect ()
dsn= "Filedsn=acces.dsn" ' Dacces.dsn as a DSN for MDB files, set up when you configure ODBC, and if you want to upload files, you can upload the access.dsn together.
Set Cnn=server.createobject ("ADODB. Connection ")" Create ADO Objects
Cnn.open DSN Open DSN file in current directory (DSN is DSN for Access file)
Response.Write "Database connection succeeded:" & cnn.state & "<br>"
Cnn.close ' Close connection
Response.Write "Database is closed" & cnn.state & "<br>"
Set cnn=nothing ' Clears variables
End Sub

'. ' "" "" "" "" "" "" "" "" "" "" "' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
Sub sel ()
dsn= ' Filedsn=acces.dsn ' DACCES.DSN is the DSN
Set Cnn=server.createobject ("ADODB") for the MDB file. Connection ") ' Creates an ADO object
Cnn.open DSN opens the DSN file in the current directory (DSN for Access file)
' and gets the result
sql= ' select * from user ' Constructs the SQL statement
set RS=CNN. Execute (SQL) ' Send SQL
' one loop to output all the records in the recordset
while not rs.eof
Values=rs (' name ')
Response.Write Values & <br>
Rs.movenext
wend
rs.close
cnn.close
Set cnn=nothing
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The following paragraph is manipulating the database (modifying the content)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Modi ()
dsn= "Filedsn=acces.dsn" ' Dacces.dsn as a DSN for an MDB file
Set Cnn=server.createobject ("ADODB. Connection ")" Create ADO Objects
Cnn.open DSN Open DSN file in current directory (DSN is DSN for Access file)
sql= "Update user set Name= ' This is the program ' where Name= ' A '" ' constructs modify SQL statements
Cnn. Execute SQL ' send out SQL
Cnn.close
Set cnn=nothing
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The following paragraph is manipulating the database (deleting content)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Del ()
dsn= "Filedsn=acces.dsn" ' Dacces.dsn as a DSN for an MDB file
Set Cnn=server.createobject ("ADODB. Connection ")" Create ADO Objects
Cnn.open DSN Open DSN file in current directory (DSN is DSN for Access file)
Sql= "Delete from user where name= ' B '" ' Construct deletes SQL statement
Cnn. Execute SQL ' send out SQL
Cnn.close
Set cnn=nothing
End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The following paragraph is manipulating the database (add content)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Add ()
dsn= "Filedsn=acces.dsn" ' Dacces.dsn as a DSN for an MDB file
Set Cnn=server.createobject ("ADODB. Connection ")" Create ADO Objects
Cnn.open DSN Open DSN file in current directory (DSN is DSN for Access file)
' sql= ' insert into user (Name,sex,note) VALUES (' TT ', ' TT ', ' TT ') ' Constructs add SQL statements
' CNN. Execute SQL ' send out SQL
Cnn.close
Set cnn=nothing
End Sub
%>



Related Article

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.