Sql select query using

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;
    }

his conclusion 0. Therefore, I am trying to find out what the problem is and run a sample request in the management studio, but the output is different from providing it 1. After trying to permute the combinations, I think the problem with this statement com.Parameters.AddWithValue("@tags", tag);may be possible is @tagsnot replaced in the request.

+4
source share
3 answers

I think your request should be

string query = "SELECT count(*) FROM [DB_us2].[dbo].[discns] where tags like @tags";

And add a wildcard to the parameter

com.Parameters.AddWithValue("@tags", "%" + tag + "%");
+6
source

Must be

 AddWithValue("@tags", "%" + tag + "%");

you need to add% s inside AddWithValue

0
source
query = " SELECT count(*) FROM [DB_us2].[dbo].[discns] where tags like '%'+ @tags + '%'";

.

0

All Articles