Page 1 of 1

Search and connect through the database [ODBC] ASP connecting to a database Rate Topic: ***** 1 Votes

#1 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Posted 30 November 2006 - 03:34 AM

Searching a database is actually a easy one. It just what i done in some tutorials
It just involves a form, and a page that displays the results.

note:
I creating my search to a simple form



Steps in searching through out the database
1. First we had to define variables
2. Searxh field
3. A database connection
4. The search and opening the data
5. Display the outputed search




Firstly to process my example, I want show that there is a form, so what i wanna do is to create a form.Below are the exmple code of the form

<table>
<tr>
<form method="POST" action="search_results.asp">
<td>Search: <input type="text" name="txtSearch" size="50"></td>
</tr>
<tr>
<td><input type="submit" value="Search"></td>
</tr>
</table>







Then we could proceed to the search

1. Define variables
		   <%
				Dim strInputSearch	'Variable for the search word
				Dim strCon				'connect to the database
				Dim adoCon			   'Database Connection Variable Object
				Dim strSQL				'SQL query for the database
				Dim rsSearch			 'search recordset storage







2. Search field

This would be variable that has the search word
strInputSearch = Request.Form("txtSearch")


'This makes it so people cant inject SQL code and/or cause some unwanted errors
strInputSearch = Replace(strInputSearch,"'", "''", 1, -1, 1)




3. Database Connection

This is setting-up the connection
Set adoCon = Server.CreateObject("ADODB.Connection")


This is that hold the connection string
strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &Server.MapPath("database.mdb")


Open the connection for the database
adoCon.Open strCon



4. Opening the search from the datase

Set the database connection
Set rsSearch = Server.CreateObject("ADODB.Recordset")


This is the SQL statement for searching.
strSQL = "SELECT tblTable.* FROM tblTable WHERE tblTable.Field LIKE '%" & strInputSearch & "%';"


Opens the database so we can get the results of the search
rsSearch.Open strSQL, adoCon



5. And then we had to display our search by means of this

'If there are no matches then continue

If rsSearch.EOF Then

   'Write out the message that there are no matches
	Response.Write("There are no matches with your search")

'If there are matches, continue
Else

	'Loop through the database to display all the matches
	DO UNTIL rsSearch.EOF

		'Write out the match found. You need to change Field to your database field you are search (its in red)
		Response.Write(rsSearch("Field") & "<br>")

	'Move to next line in database
	rsSearch.MoveNext
	'Continue looping through database to display all results found
	Loop
End If





note: as we open the database we had to corresponding action to do to close the database



here's the code

'Reset server objects
Set rsSearch = Nothing
adoCon.Close
Set adoCon = Nothing
%>






Want to add thing's as your reference, I search for more example for you.Take a sneek on it:
ADO Conneciton String
How to access SQL Server in ASP
ASP.NET connection

This post has been edited by eXceed69: 05 December 2006 - 08:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Search and connect through the database [ODBC]

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 05 December 2006 - 01:14 AM

Don't forget to make the changes, exceed69.

Good Day.
Was This Post Helpful? 0
  • +
  • -

#3 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Posted 05 December 2006 - 08:38 PM

This is been modified. I added some link to had better understanding of the topic
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1