Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

simple search use multiple words in textbox

0.00/5 (No votes)
29 Dec 2005 1  
Using simple code for searching in database

Introduction

It's always difficult when you want search in database using multiple words. That means we have textbox like "google" and you Enter Some words in textbox then click "go" ,at that time you want to search all the words  in textbox. This is really simple code for searching all tables that use VB.NET code and SQL server.

Background

For who wants to search in the database must know about text split in vb. you can do it in two ways:

1. Declare Stored Procedure in SQL then set parameters in VB then split textbox with split function.

2. Declare function and SQL string without Stored Procedure then search in database with textbox and split function.

Using the code

Here blow I first declare array with two parameters then I split the textbox with "split" function. If you using Stored Procedure you can declare parameter such as "Name" the in the Stored procedure declare SQL command.

Here is the code:

//code behind of webform1.aspx

Dim objparameter As New SqlParameter("@Name", SqlDbType.NVarChar, 50)
Dim a(2) As String
Dim j As Integer
a = txtsearch.Text.Split("", 2)
objcommand.Parameters.Add(objparameter)
objparameter.Direction = ParameterDirection.Input
For j = 0 To a.GetUpperBound(0)
objparameter.Value = a(j)
Next
//here is stored Procedure code

CREATE PROCEDURE main 
@Name nvarchar(50)

AS
select tables from computer where .... like "%"+@Name+"%"

Contact

If you have any question, or any comments, please contact me: mailto:info@articles.ir


Points of Interest

You can build your own simple search engine.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here