This can be done from a database (SQL), and it will be the best solution. You must create one Stored Procedure
for this as shown below.
SQL:
Create Proc SP_Search(@A Int, @B NVarChar(20), @C Int)
As
Begin
If @A = 0 Set @A = Null
If @B = '' Set @B = Null
If @C = 0 Set @C = Null
Select * From Table Where (A=@A Or @A Is Null) And (B=@B Or @B Is Null) And (C=@C Or @C Is Null)
End
SQL. param @A
, @B
@C
. @A
0
, @A = Null
. (A=@A Or @A Is Null)
. @A
, , null, . .
Exec SP_Search 1,'',0
- a + b
Exec SP_Search 1,'A',0
- a + b + c
Exec SP_Search 1,'A',1
- a + c
Exec SP_Search 1,'',1
- b
Exec SP_Search 0,'A',0
- b + c
Exec SP_Search 0,'A',1
- c
Exec SP_Search 0,'',1
# Stored Procedure
:
int A = 1;
string B = "A";
int C = 1;
using (SqlConnection conn = new SqlConnection("Connection String")) {
conn.Open();
SqlCommand cmd = new SqlCommand("SP_Search", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@A", A));
cmd.Parameters.Add(new SqlParameter("@B", B));
cmd.Parameters.Add(new SqlParameter("@C", C));
using (SqlDataReader reader = cmd.ExecuteReader()) {
while (reader.Read())
{
}
}
}