Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
lazarjojic
Starting Member
7 Posts |
Posted - 2012-11-11 : 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
7 Posts |
Posted - 2012-11-12 : 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 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-12 : 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
7 Posts |
Posted - 2012-11-12 : 10:07:02
|
It is ok.i answered myself on my questionlazar jojic |
|
|
|
|
|
|
|