0

I have this example code:

string query = "select * from xy where id == @id and name == @name";
SqlCommand cmd = new SqlCommand(query);
if(txtUsername.Text.Length > 0)
{
cmd.Parameters.Add["@name", SqlDbType.VarChar);
cmd.Parameters["@name"].Value = txtUsername.Text;
}

Now if the txtUsername <=0 I have to cut the query string dropping the AND and the name == @name. How can I achieve this result?

Thanks in advance.

4
  • is this really working? txtUsername.Text > 0 Commented Jul 5, 2011 at 8:15
  • 1
    @Attila really? are you sure you haven't missed a .Length? Commented Jul 5, 2011 at 8:28
  • maybe you forgot the .Length, comparing a string with an int ... Commented Jul 5, 2011 at 8:31
  • of course i forgot the length :D Commented Jul 5, 2011 at 8:46

4 Answers 4

2
bool useName = !String.IsNullOrEmpty(txtUsername.Text);
StringBuilder query = new StringBuilder("select * from xy where id=@id");
if(useName) 
 query.Append(" AND name=@name");

SqlCommand cmd = new SqlCommand(query.ToString());
// add ID param
if(useName) {
  // add name param
}
Sign up to request clarification or add additional context in comments.

3 Comments

Tnx. Do you know now how can, in the end, get the query in cmd and put it into a string? I guess string query2 = cmd.ToString() does not do the trick :P
bool useName = String.IsNullOrEmpty(txtUsername.Text); should be bool useName = !String.IsNullOrEmpty(txtUsername.Text);
@Attila cmd.CommandText holds slq statement. string query2 = cmd.CommandText;
1

You could change your query to

"SELECT * FROM xy WHERE id = @id and (@name = '' OR name = @name");

Saves messing about with your query when the parameter has no value.

2 Comments

I don't know why you'get down votes, this solution is a good solution
This is indeed a very cool solution. But only if the query optimizer is smart enough to see that the where block has a tautology in case @name is empty. Otherwise this query would have the overhead of two string comparisons. As always don't forget to check for NULL values
1
SqlCommand cmd = new SqlCommand();
if(txtUsername.Text != string.Empty)
{
cmd.CommandText = "select * from xy where id = @id and name = @name";
cmd.Parameters.Add["@name", SqlDbType.VarChar);
cmd.Parameters["@name"].Value = txtUsername.Text;
}
else
{
cmd.CommandText = "select * from xy where id = @id";
}

Comments

-1

Have a look at stringtemplate.org. ST allows for all kinds of text construction and is very flexible.

If your example is the only query that needs runtime construction then ST is overkill. But I am currently working on a project with many queries and ST made me happy. The fact that all your queries are stored in a single file and not spread out inside many C# classes is enough to make the transition to ST worthwhile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.