日期:2014-05-18 浏览次数:20762 次
sb.Append(" and student.StudentName like '%@studentName%'");
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+"%'");
------解决方案--------------------
这里 like '%@studentName%'" 不能加引号,引号会自动加上去,%你可以拼接或者写在变量里