ASP Operations Database

Source: Internet
Author: User
Code

A common way to operate databases using ASP is to use the built-in component adobd,CodeSimple and easy to use.
The following is the sample code for reading the ACCESS database. The connection object and recordset object are used.
< %
Dim Conn, connstr
Set Conn = Server. Createobject ( " ADODB. Connection " )
Connstr = " Provider = Microsoft. Jet. oledb.4.0; Data Source = "   & Server. mappath ( " DB. MDB " )
Conn. Open connstr

Dim RS
Set RS = Server. Createobject ( " ADODB. recordset " )
Rs. Open " Select * from Info order by createtime DESC " , Conn, 1 , 3
If   Not Rs. EOF Then
For I = 1   To Rs. recordcount
Response. Write (RS ( " Title " ) &   " <Br/> " )
Rs. movenext
If Rs. EOF Then   Exit   For
Next
End   If

Rs. Close
Set RS =   Nothing
Conn. Close
Set Conn =   Nothing
% >

Change the database, add a database record, and use the recordset object
<%

Dim Title
Dim Content

Title = " I am the second title "
Content = " I am the second content "

Dim Conn, connstr
Set Conn = Server. Createobject ( " ADODB. Connection " )
Connstr = " Provider = Microsoft. Jet. oledb.4.0; Data Source = "   & Server. mappath ( " DB. MDB " )
Conn. Open connstr

Set RS = Server. Createobject ( " ADODB. recordset " )
SQL = " Select * from info where 0 = 1 "  
Rs. Open SQL, Conn, 1 , 3
Rs. addnew
RS ( " Title " ) = Title
RS ( " Content " ) = Content
RS ( " Createtime " ) = Date ()
Rs. Update
Rs. Close
Set RS = Nothing
Conn. Close
Set Conn =   Nothing
% >

Change the database, modify a database record, and use the recordset object
< %
Dim ID
Dim Title
Dim Content

ID = 2 ' Initialization ID
Title = " Modify me as the second title "
Content = " Modify me as the second content "


Dim Conn, connstr
Set Conn = Server. Createobject ( " ADODB. Connection " )
Connstr = " Provider = Microsoft. Jet. oledb.4.0; Data Source = "   & Server. mappath ( " DB. MDB " )
Conn. Open connstr

Set RS = Server. Createobject ( " ADODB. recordset " )
SQL = " Select * from info where id = " & ID
Rs. Open SQL, Conn, 3 , 3
RS ( " Title " ) = Title
RS ( " Content " ) = Content
Rs. Update
Rs. Close
Set RS = Nothing
Conn. Close
Set Conn =   Nothing
% >
You can also use the following methods to add, modify, or delete a connection object only.
< %
Dim ID

ID = 2 ' Initialization ID

Dim Conn, connstr
Set Conn = Server. Createobject ( " ADODB. Connection " )
Connstr = " Provider = Microsoft. Jet. oledb.4.0; Data Source = "   & Server. mappath ( " DB. MDB " )
Conn. Open connstr

SQL = " Delete from info where id = " & ID
Conn. Execute SQL, stateflag

If Stateflag = 1   Then
Response. Write " Operation successful "
Else
Response. Write " Operation failed "
End   If
% >
The Access database connection string can also be changed
< %
Connstr = " Driver = {Microsoft Access Driver (*. mdb)}; DBQ = "   & Server. mappath ( " DB. MDB " )
% >
If the ACCESS database has a password, you only need to change the connstr to the following.
< %
Connstr = " Driver = {Microsoft Access Driver (*. mdb)}; uid = admin; Pwd = Database Password; DBQ = "   & Server. mappath ( " DB. MDB " )
% >

To operate the SQL Server database, you only need to change the connstr.
<%
Connstr="Provider = sqloledb.1; Password = sa; persist Security info = true; user id = sa; initial catalog = mydb; Data Source = lbwin7 \ sqlserver2005"
%>
Data source is the name of the computer where the database is located and the example of the database. The computer name can be an IP address or a computer name. If multiple databases are installed on the computer, the instances are different.
Lbwin7\In sqlserver2005, lbwin7 is the computer name, And sqlserver2005 is the database instance name.
Initial catalog, database name.
User ID, database logon name.
Password: the password used to log on to the database.

 

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.