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
    – Attila
    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
}
3
  • 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
    – Attila
    Commented Jul 5, 2011 at 8:17
  • bool useName = String.IsNullOrEmpty(txtUsername.Text); should be bool useName = !String.IsNullOrEmpty(txtUsername.Text); Commented Jul 5, 2011 at 8:21
  • @Attila cmd.CommandText holds slq statement. string query2 = cmd.CommandText;
    – Nika G.
    Commented Jul 5, 2011 at 8:22
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
  • I don't know why you'get down votes, this solution is a good solution Commented Jul 5, 2011 at 8:29
  • 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
    – hage
    Commented Jul 5, 2011 at 9:49
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";
}
-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.

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.