SELECT COUNT(*) FROM vw_Books_Categories_Publishers WHERE Title like '[@title]%'
这是查询语句,无论title传过来的是什么,返回的一直都是32,这是方法
public static int SelectLikeBooksCount(string title)
{
SqlParameter para = new SqlParameter("@title", title);
return Convert.ToInt32(DBHelper.ExecuteScalar(SQL_SELECT_LIKE_BOOKS_CONUT, CommandType.Text, para));
}
这是ExecuteScalar方法
public static object ExecuteScalar(string commandText, CommandType commandType, params SqlParameter[] paras)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand();
PrepareCommand(command, connection, commandText, commandType, paras);
object obj = command.ExecuteScalar();
connection.Close();
return obj;
}
}
这是PrepareCommand方法
private static void PrepareCommand(SqlCommand command, SqlConnection connection, string commandText, CommandType commandType, params SqlParameter[] paras)
        {
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            command.Connection = connection;
            command.CommandText = commandText;
            command.CommandType = commandType;
            if (paras != null)
                command.Parameters.AddRange(paras);
        }

解决方案 »

  1.   

    SELECT COUNT(*) FROM vw_Books_Categories_Publishers WHERE Title like '[@title]%' 
    这样的话 Sql Server 不会将 @title 当参数处理,你每次查询的都是 Title 包含字母 @ t i t l e 中任何一个的数据
    改成这样试试
    SELECT COUNT(*) FROM vw_Books_Categories_Publishers WHERE Title like @title + '%' 
      

  2.   

    我还想问一下WHERE Title like @title + '%' like的条件不是要在''里面吗?这里的变量没有在里面也可以的吗?