日期:2014-05-18  浏览次数:20762 次

大神们,帮看下这个查询SQL语句有什么问题。

前两个参数没有问题,可是最后一个参数怎么也得不到数据。  
就是
C# code
sb.Append(" and student.StudentName like '%@studentName%'");
不知有什么问题?

谢谢各位大神了!!!

C# code

public List<Result> SearchStudentResultByGradeIdAndSubjectIdAndStudentName(string gradeId, string subjectId, string studentName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select result.StudentNo,result.SubjectNo,Result.studentResult,ExamDate from "
                                     +"Result join subject on result.SubjectNo=Subject.SubjectNo join Student on "
                                     +"Result.StudentNo=Student.StudentNo where 1=1");
            if (gradeId != "-1")
            {
                sb.Append(" and Subject.GradeId=@gradeId");
            }
            if (subjectId != "-1")
            {
                sb.Append(" and Subject.SubjectNo=@subjectId");
            }
            if (studentName != "")
            {
                sb.Append(" and student.StudentName like '%@studentName%'");
            }

            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@gradeId",gradeId),
                new SqlParameter("@subjectId",subjectId),
                new SqlParameter("@studentName",studentName)
            };



------解决方案--------------------
sb.Append(" and student.StudentName like '%"+@studentName+"%'");

------解决方案--------------------
SQL code
sb.Append(" and student.StudentName like '%"+@studentName+"%'");

------解决方案--------------------
这里 like '%@studentName%'" 不能加引号,引号会自动加上去,%你可以拼接或者写在变量里