我正在尝试运行此代码

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

其给定输出0。所以我尝试找出问题所在,并在Management Studio上运行示例查询,但是输出与给定的1不同。在尝试所有排列组合之后,我认为问题在于此语句com.Parameters.AddWithValue("@tags", tag);可能是在查询中未替换@tags的问题。

最佳答案

我认为您的查询应该是

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

并将通配符添加到参数
com.Parameters.AddWithValue("@tags", "%" + tag + "%");

关于c# - SQL选择查询使用喜欢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19584186/

10-09 08:00
查看更多