Generalized Search Function

Hello All

I am looking for some approach to buid a Generalised search function which can cater to multiple search parameter combinations.

For example
string Account,string Server,string UserID, string UserName, string ActionRequired, string SupportGroup

The above are the search parameters with which i need to create more than 12 combination and for each combination i have to write one IF loop.

Thus kindly help me with some way/approach thru which i can generalize the approach

thanks
Senz
LVL 4
Senz79Asked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
Use dynamic SQL.
Pass all the parameters in, NULL if not present to an SP.
In the SP, progressively build the statement - this gets around parameter sniffing issues.

This example should get you started.
create proc searchanyfield
@Account varchar(100),
@Server varchar(100),
@UserID int
AS

declare @sql nvarchar(max)
-- ignore the 1=1. It's there to force a "WHERE" clause
set @sql = 'select * from mytable where 1=1 '

if @Account is not null
  set @sql = @sql + ' and Account = @Account'
if @Server is not null
  set @sql = @sql + ' and Server= @Server'
if @UserID is not null
  set @sql = @sql + ' and UserID = @UserID'

exec sp_executesql @sql,N'@Account varchar(100),@Server varchar(100),@UserID int',
    @Account,@Server,@UserID
GO

Open in new window

0
 
thepanchCommented:
can you pls put an example of what you are doing? i really dont understand your question
0
 
anarki_jimbelCommented:
Indeed it's not very clear what you want but if I'm not wrong, you just need to construct a search string based on some selections.

Have a look at my code. It's not tested at run time (I didn't bother to setup database) but idea should be clear.
        private void button3_Click(object sender, EventArgs e)
        {

            List<KeyValuePair<string, string>> searchParams = new List<KeyValuePair<string,string>>();
            if (serachByComapany)
            {
                KeyValuePair<string, string> pair = new KeyValuePair<string, string>();
                pair.Key = "Company";
                pair.Value = txtCompanyName.Text;
            }

            //....
            SqlDataReader reader = getDataReader(searchParams);
            if (reader != null)
            {
                while (reader.Read())
                {
                    //...
                }
            }
        }

        private SqlDataReader getDataReader(List<KeyValuePair<string, string>> searchParams)
        {

            SqlCommand cmd = new SqlCommand();
            string searchStr = "Select * FROM MyTable WHERE ";

            for (int i = 0; i < searchParams.Count; i++)
            {
                searchStr = searchStr + searchParams[i].Key + " = @" + searchParams[i].Key;
                if (i < searchParams.Count - 1) 
                {
                    searchStr = searchStr + " AND ";
                }
                // 2. define parameters used in command object
                SqlParameter sqlParam = new SqlParameter();
                sqlParam.ParameterName = "@" + searchParams[i].Key;
                sqlParam.Value = searchParams[i].Key;
                cmd.Parameters.Add(sqlParam);
            }

            SqlConnection conn = null;
            SqlDataReader reader = null;
            cmd.CommandText = searchStr;
            cmd.Connection = conn;
            try
            {
                conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
                conn.Open();
                reader = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                return reader;
            }


        }

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
puru1981Commented:
you can use a searchCriteria class and set the values to the class and then pass the class to the called function(readability). here you can see for the member variable and its value.

secondly you can use a hashtable to store the name value pair and in called function iterate through the collection and create the dynamic string for search criteria using name and value.
0
 
Senz79Author Commented:
Hey Cyberkiwi
Can you elaborate your solution with the below parameters
string Account,string Server,string UserID, string UserName, string ActionRequired, string SupportGroup
 
0
 
Senz79Author Commented:
I urge the administrator to block these kind of message which gives us a negetive impact on our discussion.
0
 
thepanchCommented:
Answering to your problem, the thing anarki was saying is on sql you set the default value of the variable to null, so if you dont send it, the "join" statment doesnt get inside your query.
0
 
cyberkiwiCommented:
lines 2-4: sets up @Account, @Server, @UserID  with the correct types
lines 11-16:  adds the criteria to the SQL

Give it a try with the remaining 3 - string UserName, string ActionRequired, string SupportGroup

And show the SQL if you cannot get it right.
0
 
Senz79Author Commented:
Hi CyberWiki
can you help me manipulate "and"
 
SELECT Account,Server,Userid,UserName,LastLogin,ActionRequired,AccountStatus,Owner,Classification,ModuleID,CheckID,Description,HardenStatus,Support_group_name,support_group_location,Upload_Date from ALLUsers Where
and Account = @Account -- error
Please review the SP

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[searchanyfield]
@Account varchar(100),
@Server varchar(100),
@UserID varchar(100),
@UserName varchar(100),
@ActionRequired varchar(100),
@SupportGroup varchar(100)
AS

declare @sql nvarchar(max)
set @sql = 'SELECT Account,Server,Userid,UserName,LastLogin,ActionRequired,AccountStatus,Owner,Classification,ModuleID,CheckID,Description,HardenStatus,Support_group_name,support_group_location,Upload_Date from ALLUsers Where'

if @Account is not null
  set @sql = @sql + ' and Account = @Account'
if @Server is not null
  set @sql = @sql + ' and Server= @Server'
if @UserID is not null
  set @sql = @sql + ' and UserID = @UserID'
if @UserName is not null
  set @sql = @sql + ' and UserName = @UserName'
if @ActionRequired is not null
  set @sql = @sql + ' and ActionRequired = @ActionRequired'
if @SupportGroup is not null
  set @sql = @sql + ' and Support_group_name = @SupportGroup'

print @sql

exec sp_executesql @sql,N'@Account varchar(100),@Server varchar(100),@UserID varchar(100),@UserName varchar(100),@ActionRequired varchar(100),@SupportGroup varchar(100)',
 @Account,@Server,@UserID,@UserName,@ActionRequired,@SupportGroup

Open in new window

0
 
thepanchCommented:
initialize all the parameters in the  main section as null, so that way you dont need to send null on the code, the rest of the code you are using a where without a condition, so i suggest you to set the first where with a constant like userstatus=active and then the rest of the if statment.

or

check where the first "and" and dont put AND
0
 
cyberkiwiCommented:
Just one correction:

set @sql = 'SELECT Account,Server,Userid,UserName,LastLogin,ActionRequired,AccountStatus,Owner,Classification,ModuleID,CheckID,Description,HardenStatus,Support_group_name,support_group_location,Upload_Date from ALLUsers Where 1=1 '    --- note the space after 1=1

The 1=1 is there so that you don't have to determine when to use WHERE or to use AND (if where is already used).  Just a minor trick.

Regards
0
 
Senz79Author Commented:
This worked for me....
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[searchanyfield]
@Account varchar(100),
@Server varchar(100),
@UserID varchar(100),
@UserName varchar(100),
@ActionRequired varchar(100),
@SupportGroup varchar(100)
AS
declare @sql nvarchar(max)
set @sql = 'SELECT Account,Server,Userid,UserName,LastLogin,ActionRequired,AccountStatus,Owner,Classification,ModuleID,
CheckID,Description,HardenStatus,Support_group_name,support_group_location,Upload_Date from ALLUsers Where'
if @Account is not null
set @sql = @sql + ' Account = @Account and'
if @Server is not null
set @sql = @sql + ' Server= @Server and '
if @UserID is not null
set @sql = @sql + ' UserID = @UserID and '
if @UserName is not null
set @sql = @sql + ' UserName = @UserName and '
if @ActionRequired is not null
set @sql = @sql + ' ActionRequired = @ActionRequired and '
if @SupportGroup is not null
set @sql = @sql + ' Support_group_name = @SupportGroup and'
set @sql = SUBSTRING(@sql,0,LEN(@sql)-3)
--print @sql
exec sp_executesql @sql,N'@Account varchar(100),@Server varchar(100),@UserID varchar(100),@UserName varchar(100),@ActionRequired varchar(100),@SupportGroup varchar(100)',
@Account,@Server,@UserID,@UserName,@ActionRequired,@SupportGroup
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.