| Author |
Topic  |
|
|
lazarjojic
Starting Member
Yugoslavia
7 Posts |
Posted - 11/11/2012 : 09:01:21
|
I have form and few textboxes(id,name,last name..) on it.That form is for searching some accounts in my database.If i enter some text in taextbox name, sql query needs to search accounts with that specific name, but if i dont enter text at all query needs to search all names in database.It also aplies to all other textfields?
lazar jojic |
|
|
lazarjojic
Starting Member
Yugoslavia
7 Posts |
Posted - 11/12/2012 : 03:32:35
|
It's like this in C#.
SqlCommand cmd = new SqlCommand("SELECT * FROM accounts WHERE (id=@id2 OR @id2 is null) AND (name=@name2 or @name2 is null) AND (last_name=@last_name2 or @last_name2 is null)",con);
if (id.Text.ToString() == "") cmd.Parameters.AddWithValue("id2", DBNull.Value); else cmd.Parameters.AddWithValue("id2", id.Text); if (name.Text.ToString() == "") cmd.Parameters.AddWithValue("name2", DBNull.Value); else cmd.Parameters.AddWithValue("name2", ime.Text); if (last_name.Text.ToString() == "") cmd.Parameters.AddWithValue("last_name2", DBNull.Value); else cmd.Parameters.AddWithValue("last_name2", last_name.Text);
lazar jojic |
Edited by - lazarjojic on 11/12/2012 03:33:31 |
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 11/12/2012 : 06:55:33
|
The query and the code you have posted, at least conceptually, seems fine. What is the issue you are facing? Is it not working as expected?
There is one problem you may run into - and that relates to poor performance with this type of "Catch-all" queries. See Gail Shaw's blog here for a discussion and possible solutions: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/ |
 |
|
|
lazarjojic
Starting Member
Yugoslavia
7 Posts |
Posted - 11/12/2012 : 10:07:02
|
It is ok.i answered myself on my question
lazar jojic |
Edited by - lazarjojic on 11/12/2012 10:07:56 |
 |
|
| |
Topic  |
|