首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-09-09 
大神们,帮看下这个查询SQL语句有什么问题。前两个参数没有问题,可是最后一个参数怎么也得不到数据。就是C# c

大神们,帮看下这个查询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%'" 不能加引号,引号会自动加上去,%你可以拼接或者写在变量里

热点排行