Reputation: 1205
I am trying to run this code
public long getTopicCountWithTag(String tag)
{
long count;
query = " SELECT count(*) FROM [DB_us2].[dbo].[discns] where tags like '%@tags%'";
try
{
com = new SqlCommand(query, con);
com.Parameters.AddWithValue("@tags", tag);
con.Open();
sdr = com.ExecuteReader();
sdr.Read();
count= sdr.GetInt32(0);
}
catch (Exception e)
{
count = -1;
throw e;
}
finally
{
con.Close();
}
return count;
}
its giving output 0
. So i try figure out what is the problem and run sample query on management studio but output is different its giving 1
. After trying all permutation combination, i think problem is with this statement com.Parameters.AddWithValue("@tags", tag);
might be possible @tags
is not replaced in query.
Upvotes: 5
Views: 20004
Reputation: 2218
query = " SELECT count(*) FROM [DB_us2].[dbo].[discns] where tags like '%'+ @tags + '%'";
and leave everething as it was.
Upvotes: 0
Reputation: 2495
Should be
AddWithValue("@tags", "%" + tag + "%");
you have to bring %s inside AddWithValue
Upvotes: 0
Reputation: 33839
I think your query should be
string query = "SELECT count(*) FROM [DB_us2].[dbo].[discns] where tags like @tags";
And add the wildcard to the parameter
com.Parameters.AddWithValue("@tags", "%" + tag + "%");
Upvotes: 10