Novice Learn ADO Instance tutorial ADO query data

Source: Internet
Author: User
ado| Tutorials | data

Site original content, reproduced please indicate the source Web page teaching network .

We can use SQL statements to create a query that shows the data in the database we need by condition.

For example, the name of the company that displays "Companyname" starting with a. The following code can be implemented:

<body>

<%

Set Conn=server.createobject ("ADODB. Connection ")

Conn. Provider= the "microsoft.jet.oledb.4.0"

Conn. Open "E:/webjx/webjx.mdb"

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

Sql= Select Companyname, ContactName from Customers WHERE CompanyName like ' a% '

Rs. Open SQL, conn

%>

<table border= "1" width= "100%"

<tr>

<%for each x in Rs. Fields

Response.Write ("<th>" & X.name & "</th>")

Next%>

</tr>

<%do until Rs. Eof%>

<tr>

<%for each x in Rs. Fields%>

<td><%response.write (x.value)%></td>

<%next

Rs. Movenext%>

</tr>

<%loop

Rs.close

Conn.close%>

</table>

</body>

Displays information for companies with the start letter greater than E in "Companyname":

<body>

<%

Set Conn=server.createobject ("ADODB. Connection ")

Conn. Provider= "Microsoft.Jet.OLEDB.4.0"

Conn. Open (Server.MapPath ("E:/webjx/webjx.mdb"))

Set rs = Server.CreateObject ("Adodb.recordset")

Sql= "Select Companyname, ContactName from Customers WHERE companyname> ' E '"

Rs. Open SQL, Conn

%>

<table border= "1" width= "100%" >

<tr>

<%for each x in Rs. Fields

Response.Write ("<th>" & X.name & "</th>")

Next%>

</tr>

<%do until Rs. Eof%>

<tr>

<%for each x in Rs. Fields%>

<td><%response.write (x.value)%> </td>

<%next

Rs. Movenext%>

</tr>

<%loop

Rs.close

Conn.close

%>

</table>

</body>

Information about the company that displays "Country" as:

<body>

<%

Set Conn=server.createobject ("ADODB. Connection ")

Conn. Provider= "Microsoft.Jet.OLEDB.4.0"

Conn. Open (Server.MapPath ("E:/webjx/webjx.mdb"))

Set rs = Server.CreateObject ("Adodb.recordset")

Sql= "Select Companyname, ContactName from Customers WHERE country= '"

Rs. Open SQL, Conn

%>

<table border= "1" width= "100%" >

<tr>

<%for each x in Rs. Fields

Response.Write ("<th>" & X.name & "</th>")

Next%>

</tr>

<%do until Rs. Eof%>

<tr>

<%for each x in Rs. Fields%>

<td><%response.write (x.value)%> </td>

<%next

Rs. Movenext%>

</tr>

<%loop

Rs.close

Conn.close

%>

</table>

</body>

The user chooses the country and then displays the appropriate information:

<body>

<%

Set Conn=server.createobject ("ADODB. Connection ")

Conn. Provider= "Microsoft.Jet.OLEDB.4.0"

Conn. Open (Server.MapPath ("E:/webjx/webjx.mdb"))

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

Sql= "Select DISTINCT Country from Customers order by Country"

Rs. Open Sql,conn

Country=request.form ("Country")

%>

<form method= "POST" >

Select Country: <select name= "Country" >

<% do Until Rs. Eof

Response.Write ("<option")

If Rs.fields ("Country") =country Then

Response.Write ("selected")

End If

Response.Write (">")

Response.Write (Rs.fields ("Country"))

Rs. MoveNext

Loop

Rs. Close

Set Rs=nothing%>

</select>

<input type= "Submit" value= "Show info" >

</form>

<%

If country<> "" Then

Sql= "Select Companyname,contactname,country from Customers WHERE country= '" & Country & "'"

Set Rs=server.createobject ("ADODB. Recordset ")

Rs. Open Sql,conn

%>

<table width= "100%" cellspacing= "0" cellpadding= "2" border= "1" >

<tr>

<th> Company Name </th>

<th> Contact Person </th>

<th> National </th>

</tr>

<%

Do Until Rs. Eof

Response.Write ("<tr>")

Response.Write ("<td>" & Rs.fields ("CompanyName") & "</td>")

Response.Write ("<td>" & Rs.fields ("ContactName") & "</td>")

Response.Write ("<td>" & Rs.fields ("Country") & "</td>")

Response.Write ("</tr>")

Rs. MoveNext

Loop

Rs.close

Conn. Close

Set rs=nothing

Set conn=nothing%>

</table>

<% End If%>

</body>



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.