ASP connection read-write access DB instance

Source: Internet
Author: User

(i) Selection of databases:

There are a number of databases you can choose from, SQL SERVER, ACCESS (*.mdb), EXCEL (*.xls), FOXPRO (*.dbf), and even ordinary text files (*.txt) can all achieve the ability to store data. So what is the most appropriate database to choose? SQL Server, which is servers-level, is sufficient to handle tens of thousands of traffic per day. ACCESS97 is recommended for non-server-level databases.

Because:
(1) As long as PWS is installed or IIS4 must have access ODBC driver, that is, your system will definitely support Access database access.
(2) Access supports a very complete array of SQL instructions.
(3) ACCESS ODBC drivers are highly efficient. Although it is not a server-level database, it is more than sufficient to support the database as a small to Medium business intranet.
(4) ACCESS97 in the OFFICE97, it is very convenient to get and use.

In this paper, we introduce the database access method based on the Access database.

(b) Example: We create a "customer" database, we need to keep the following customer information: Customer name, contact person name, city, phone number.

(1) First establish Access database Customer.mdb (useful in the future).
Create the following fields: Customer name, contact name, city, phone number, customer number, time. Save the table as a "customer" (a useful,===> look at the chart later). Note: There are 2 more (customer numbers, times) when you create a field because they are used in future sorting. It is advisable to add these 2 items to the database in the future, and it will not be useless. In addition, when you save the table, Access asks if you want to create a live keyword, and we don't need it to be established. Also: the "Default value" of the Time field is set to "now ()", that is, the time to go to the system. At the same time, the index entry is set to "have (allow duplicates)", meaning: The web is very likely to access data at the same time, so allow duplication, the index is set to "have" can speed up the sorting speed. ===> look at the picture

(2) To establish a submission form add.htm:

The source code is as follows:

<title> Add database Records </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" >
<p align= "center" ><font size= "5" > Add database Records </font></p>
<form method= "POST" action= "adddatabase.asp" >
<div align= "center" >
<table width= "46%" border= "0" >
<tr>
&LT;TD width= "28%" >
<div align= "Right" > Company Name: </div>
</td>
&LT;TD width= "72%" >
<input type= "Text"name= "CompanyName">
</td>
</tr>
<tr>
&LT;TD width= "28%" >
<div align= "Right" > Contact person Name: </div>
</td>
&LT;TD width= "72%" >
<input type= "Text"name= "CName">
</td>
</tr>
<tr>
&LT;TD width= "28%" >
<div align= "Right" > City: </div>
</td>
&LT;TD width= "72%" >
<input type= "Text"name= "City">
</td>
</tr>
<tr>
&LT;TD width= "28%" >
<div align= "Right" > Phone number:</div>
</td>
&LT;TD width= "72%" >
<input type= "Text"name= "Tel">
</td>
</tr>
<tr>
&LT;TD width= "28%" >
<div align= "right" > </div>
</td>
&LT;TD width= "72%" >
<input type= "Submit" name= "submit" value= "Submit" >
<input type= "reset" name= "reset" value= "reset" >
</td>
</tr>
</table>
</div>
</form>
</body>

This is no different from normal HTML, and the key parts are already marked in red. To submit the method with post, the post-processing procedure is adddatabase.asp (the next step is to focus on it).

(3) Key part: Establish a contact with the database and add the information collected in the form to the Access database.

The ASP code is placed between 〈% and%>.

Adddatabase.asp Program content:

<%
CompanyName =request ("CompanyName")
CNAME =request ("CNAME")
City =request ("City")
Tel = Request ("tel")
%>
Above, first obtains the data which fills in the form, uses the ASP Request object very easily can realize the data collection. In double quotes is the name of each input box created in the previous file.Red Section (name=)。 So it's a good habit to name each input box in the future, although sometimes it doesn't have to be used. The data obtained is stored in the variable of the equal-seated side of the equals sign.

If CompanyName = "or CName =" "or city =" "or Tel =" Then
Response.Write "' Response.Redirect ' error.htm "
Response.End
End If

This is to detect whether the item entered something, if it is empty do not proceed down, display error message. Note: You can use both methods. I've listed all of them.
Method One: Use the Response.Write method to write an error page, similar to the Java scripts document.write notation, just ASP use Response.Write.
Method Two: Use the Response.Redirect method to guide the error page error.htm. I put a single quotation mark in front of the code of method one to denote the comment. (‘single quotes for annotations in ASP‘)。

<%
Setdbconnection=server.createobject ("ADODB. CONNECTION ")
DBPath =server.mappath ("Customer.mdb")
DbConnection. Open "Driver={microsoft Access driver (*.mdb)};d bq=" & DBPath
%>

Program Explanation:
(1) Establishing a Connection object: DbConnection, establishing a connection (Connection) is the beginning of database access. We use the ASP's built-in object method Server.CreateObject to establish a connection to the database. ADODB is an ASP database access component, do not have to worry about it as long as there is ASP, is built-in.
(2) Use the Dbconnection.open method to open the database Customer.mdb. Driver parameter syntax: Driver={microsoft Access driver (*.mdb)} must be written like this. Tells the system to use Access ODBC driver processing. The DBQ parameter is used to specify the open database file, which must be the full path. For example "C:\inetpub\wwwroot\customer\customer.mdb", we use the Server.MapPath method to get the full path of the database.

<%
Set rs =server.createobject ("ADODB. Recordset ")
%>

Set up the Recordset object Rs. Connection only establishes a connection to the database, and the Recordset object should be established to access the data.

Rs. Open "Customer", Dbconnection,adopendynamic, 3

Open the table in the database "customer" (the customer is the table name). Grammar:

Rs.Open data table name or SQL instruction, already established connection object (here is DbConnection), lock type

<%
Rs. AddNew
RS ("Company name") =companyname
RS ("Contact person name") = CName
RS ("location") = City
RS ("phone number") = Tel
Rs. Update
%>

Add data to the database request with AddNew. Finally write with update. Note: The variable on the right side of the equal sign holds the form data obtained with request. In double quotation marks is the database field, should be consistent with the database AH. Otherwise, it will go wrong. The data in this variable is written in the corresponding field in the database.

Response.Write "Response.Write "<a href= ' viewdatabase.asp ' > View Data </a>"
Response.Write "</font></center></body>
' After successful writing, display the congratulations message and establish a connection to view the database records.

%>

(4) Show/View database records:

<%
Setdbconnection=server.createobject ("ADODB. CONNECTION ")
DBPath =server.mappath ("Customer.mdb")
DbConnection. Open "Driver={microsoft Access driver (*.mdb)};d bq=" & DBPath

' Establish a connection with the database, with the same.

Sql= "SELECT * from Customer Order by Time DESC"
Setcustomerlist=dbconnection. EXECUTE (SQL)
%>

Create a SQL query statement that queries all records from the Customers table, and the order by time Desc means "Sort in descending order of time," so that the last added information is displayed at the top. The following sentence executes the SQL query and returns the result to the Recordset object customerlist.

<body>
<fontsize= "5" ><center> database records </center></FONT>
<CENTER>
<table border=1>
<tr>

<td> numbering </td>
<td> Company Name </td>
<td> Contact person's name </td>
<td> City </td>
<td> Phone number </td>
</tr>
<% Do While notcustomerlist.eof%>

' The Test record is on the last line. EOF represents the end of File.

<TR>
<td><%=customerlist ("customer number")%></td>
<td><%=customerlist ("Company name")%></td>
<td><% =customerlist ("Contact person name")%></td>
<td><%=customerlist ("City")%></td>
<td><%=customerlist ("phone number")%></td>
</TR>

<%=customerlist ("customer number")%> can be said to be shorthand for the Response.Write method. Writes the data in the customer Number field to the TD label.

<%

Customerlist.movenext

Loop

%>

If you have not yet reached the last bar, the pointer moves to the next bar. With Do While ... Loop loops to get all the records.

</TABLE>
</CENTER>


<center>
<inputtype= "button" onclick= "Java script:location.href= ' add.htm '" value= "Add Data" >
</center>
</BODY>
</HTML>

Rs. OPEN sql,conn,a,b

A:

adOpenForwardOnly (=0)
Read-only, and the current data record can only move Down

adOpenKeyset (=1)
Read-only, the current data record can be moved freely

adOpenDynamic (=2)
Can read and write, the current data record can be moved freely

adOpenStatic (=3)
Can read and write, current data record can move freely, can see new record

B:

adLockReadOnly (=1)
Default lock type, Recordset is read-only and record cannot be modified

adLockPessimistic (=2)
Pessimistic locking, when a record is modified, the data provider attempts to lock the record to ensure that the record is successfully edited. Lock the record as soon as you start editing it.

adLockOptimistic (=3)
Optimistic locking will not lock the record until the update record is submitted with the updated method.

adLockBatchOptimistic (=4)
Batch optimistic locking, which allows multiple records to be modified, only locks the record after calling the UpdateBatch method.

When you do not need to change any records, you should use a read-only recordset so that the provider does not have to do any testing.
For general use, optimistic locking may be the best option because records are only locked for a short period of time,
The data is updated during this time period. This reduces the use of resources.
===========================================================

Database connection:

<%

Set Conn=server.createobject ("Adodb.connection")

Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Database name")

%>

Open the database:

Exec= "SELECT * From database table"

Setrs=server.createobject ("Adodb.recordset")

Rs.Open exec,conn,1,1

The parameter is read

Read content format: <%=rs ("field")%>

To add a record handler:

<%

Setconn=server.createobject ("Adodb.connection")

Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Database name")

Name=request.form ("field") name,tel,message the field value set for the submission form

Tel=request.form ("field")

Message=request.form ("field")

exec= "INSERT into table name (field) VALUES (' + field + ')" multiple separated by commas

Conn.execute exec commits using execute

Conn.close

Set conn=nothing

%>

Search handlers:

<%

Name=request.form ("field") Name,tel the field value set for the submission form

Tel=request.form ("field")

Setconn=server.createobject ("Adodb.connection")

Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Database name")

Exec= "SELECT * from table where name= '" + Field + "' and tel=" + fields

Setrs=server.createobject ("Adodb.recordset")

Rs.Open exec,conn,1,1

%>

' page to search for content exported to

<%

Do and not rs.eof

%><tr>

<td><%=rs ("name")%></td>

<td><%=rs ("tel")%></td>

<td><%=rs ("Time")%></td>

</tr>

<%

Rs.movenext

Loop

%>

To delete a record handler:

<%

Setconn=server.createobject ("Adodb.connection")

Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Database name")

exec= "Delete * from table name where number =" &request.form ("id")

Conn.execute exec

%>

To modify a record handler:

<%

Setconn=server.createobject ("Adodb.connection")

Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Database name")

Exec= "SELECT * from table name where number =" &request.form ("id")

Set Rs=server.createobject ("Adodb.recordset")

Rs.Open exec,conn,1,3 ' 1,3 for modifying meaning

RS ("name") =request.form ("field") ' Name,tel,message the field value set for the submission form

RS ("tel") =request.form ("field")

RS ("message") =request.form ("field")

Rs.update

Rs.close

Set rs=nothing

Conn.close

Set conn=nothing

%>

Modify the Record execution program: enter ID number page >>> export relative ID data >>>>>> directly modified handler

Background Login Handler Example:

<%

Dim Name,password

Name=request.form ("name")

Password=request.form ("password")

Dim exec,conn,rs

exec= "Select *from table name where (name= '" & Field & "' and Password= '" & Field & "')"

Setconn=server.createobject ("Adodb.connection")

Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Database name")

Setrs=server.createobject ("Adodb.recordset")

Rs.Open Exec,conn

If not rs.eof then

Rs. Close

Conn. Close

Session ("checked") = "yes"

Session ("check") = "right"

Response. Redirect "Index.asp"

Else

Session ("checked") = "No"

Session ("check") = "wrong"

Response. Redirect "Login.asp"

End If

%>

Each background page adds:

<%if notsession ("checked") = "Yes" then "session" defines a checked string variable

Response. Redirect "Login.asp"

Else

%>

ASP connection read/write access db instance (GO)

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.