事情是这样的:我在ASPX页面上写了一个UPDATE功能,点击按钮就提交,调用了DAL里的一个UpdateSupervisorScore操作,然后再调用SQLHelper里的ExecuteNonQuery方法。
现在的结果是:无法UPDATE,我测试了是不是UPDATE的ID为空,测试了ExecuteNonQuery的返回值,最后还使用了try catch来获取异常。ID不为空,ExecuteNonQuery返回了1,try catch没有捕获异常。一切都正常,但就是无法UPDATE。
我很奇怪,所以把代码发上来请教大家。
//ASPX.CS代码: protected void btnUpdateTeacher_Click(object sender, EventArgs e) { ss4update.SSID = Request.QueryString["ssid"].ToString(); ss4update.SchoolYear = txtSchoolYear.Text; //学年 ss4update.Semester = txtSemester.Text; //学期 ss4update.Appliant = txtAppliant.Text; //填表人 ss4update.ApplicationDate = txtApplicationDate.Text; //填表日期 ss4update.Department = txtDepartment.Text; //系部 ss4update.CourseName = txtCourseName.Text; //课程名 ss4update.Teacher = txtTeacher.Text; //授课教师 ss4update.WorkID = txtWorkID.Text; //教师工号 ss4update.Class = txtClass.Text; //授课班级 ss4update.TeachingRoom = txtTeachingRoom.Text; //授课地点 ss4update.TeachingWeek = txtTeachingWeek.Text; //第几周 ss4update.TeachingDay = txtTeachingDay.Text; //星期几 ss4update.TeachingSection = txtTeachingSection.Text; //第几节课 ss4update.Radio01 = RadioButtonList1.SelectedValue; //管理学生课堂纪律情况 ss4update.Radio02 = RadioButtonList2.SelectedValue; //及时纠正,严格要求情况 ss4update.Radio03 = RadioButtonList3.SelectedValue; //教师为人师表情况 ss4update.Radio04 = RadioButtonList4.SelectedValue; //教学内容提炼、加工 ss4update.Radio05 = RadioButtonList5.SelectedValue; //内容深浅程度 ss4update.Radio06 = RadioButtonList6.SelectedValue; //高职特色 ss4update.Radio07 = RadioButtonList7.SelectedValue; //信息量 ss4update.Radio08 = RadioButtonList8.SelectedValue; //反映本专业新技术、新动态情况 ss4update.Radio09 = RadioButtonList9.SelectedValue; //采用教学手段 ss4update.Radio10 = RadioButtonList10.SelectedValue; //使用效果 ss4update.Radio11 = RadioButtonList11.SelectedValue; //教学语言 ss4update.Radio12 = RadioButtonList12.SelectedValue; //条理、层次 ss4update.Radio13 = RadioButtonList13.SelectedValue; //教学重点 ss4update.Radio14 = RadioButtonList14.SelectedValue; //运用启发式教学方法 ss4update.Radio15 = RadioButtonList15.SelectedValue; //讲、练结合 ss4update.Radio16 = RadioButtonList16.SelectedValue; //教学环节 ss4update.Radio17 = RadioButtonList17.SelectedValue; //教学互动 ss4update.Radio18 = RadioButtonList18.SelectedValue; //课堂气氛、秩序 ss4update.Radio19 = RadioButtonList19.SelectedValue; //本次课教学目标、要求达到情况 ss4update.Radio20 = RadioButtonList20.SelectedValue; //学生参与教学活动情况 ss4update.Creative = txtCreative.Text; //创新教育 ss4update.Reform = txtReform.Text; //改革教学方法成效显著 ss4update.StuNum = txtStuNum.Text; //大班上课 if (!String.IsNullOrEmpty(ss4update.SSID.Trim())) { try { int UpdateResult = supervisorscoredal.UpdateSupervisorScore(ss4update); } catch (Exception ex) { Console.WriteLine("{0} 异常信息!", ex); } //Response.Redirect("SupervisorTeacherList.aspx?ssid=" + ss4update.SSID); } else Response.Write("修改打分操作出错!"); }
//DAL代码 public int UpdateSupervisorScore(SupervisorScoreModel supervisorscore) { string sql = @"UPDATE [supervisorscore] SET " + "[WorkID]=@WorkID,[SchoolYear]=@SchoolYear,[Semester]=@Semester,[Appliant]=@Appliant," + "[ApplicationDate]=@ApplicationDate,[Department]=@Department,[CourseName]=@CourseName,[Teacher]=@Teacher,[Class]=@Class," + "[TeachingRoom]=@TeachingRoom,[TeachingWeek]=@TeachingWeek,[TeachingDay]=@TeachingDay,[TeachingSection]=@TeachingSection," + "[Radio01]=@Radio01,[Radio02]=@Radio02,[Radio03]=@Radio03,[Radio04]=@Radio04,[Radio05]=@Radio05," + "[Radio06]=@Radio06,[Radio07]=@Radio07,[Radio08]=@Radio08,[Radio09]=@Radio09,[Radio10]=@Radio10," + "[Radio11]=@Radio11,[Radio12]=@Radio12,[Radio13]=@Radio13,[Radio14]=@Radio14,[Radio15]=@Radio15," + "[Radio16]=@Radio16,[Radio17]=@Radio17,[Radio18]=@Radio18,[Radio19]=@Radio19,[Radio20]=@Radio20," + "[Creative]=@Creative,[Reform]=@Reform,[StuNum]=@StuNum,[SupervisorScore]=@SupervisorScore,[Cntnt]=@Cntnt " + "WHERE [SSID]=@SSID"; SqlParameter[] parameters = { //异常详细信息: System.Data.SqlClient.SqlException: 变量名 '@SSID' 已声明。变量名在查询批次或存储过程内部必须唯一。 //new SqlParameter("@SSID", supervisorscore.SSID), new SqlParameter("@WorkID", supervisorscore.WorkID), new SqlParameter("@SchoolYear", supervisorscore.SchoolYear), new SqlParameter("@Semester", supervisorscore.Semester), new SqlParameter("@Appliant", supervisorscore.Appliant), new SqlParameter("@ApplicationDate", supervisorscore.ApplicationDate), new SqlParameter("@Department", supervisorscore.Department), new SqlParameter("@CourseName", supervisorscore.CourseName), new SqlParameter("@Teacher", supervisorscore.Teacher), new SqlParameter("@Class", supervisorscore.Class), new SqlParameter("@TeachingRoom", supervisorscore.TeachingRoom), new SqlParameter("@TeachingWeek", supervisorscore.TeachingWeek), new SqlParameter("@TeachingDay", supervisorscore.TeachingDay), new SqlParameter("@TeachingSection", supervisorscore.TeachingSection), new SqlParameter("@Radio01", supervisorscore.Radio01), new SqlParameter("@Radio02", supervisorscore.Radio02), new SqlParameter("@Radio03", supervisorscore.Radio03), new SqlParameter("@Radio04", supervisorscore.Radio04), new SqlParameter("@Radio05", supervisorscore.Radio05), new SqlParameter("@Radio06", supervisorscore.Radio06), new SqlParameter("@Radio07", supervisorscore.Radio07), new SqlParameter("@Radio08", supervisorscore.Radio08), new SqlParameter("@Radio09", supervisorscore.Radio09), new SqlParameter("@Radio10", supervisorscore.Radio10), new SqlParameter("@Radio11", supervisorscore.Radio11), new SqlParameter("@Radio12", supervisorscore.Radio12), new SqlParameter("@Radio13", supervisorscore.Radio13), new SqlParameter("@Radio14", supervisorscore.Radio14), new SqlParameter("@Radio15", supervisorscore.Radio15), new SqlParameter("@Radio16", supervisorscore.Radio16), new SqlParameter("@Radio17", supervisorscore.Radio17), new SqlParameter("@Radio18", supervisorscore.Radio18), new SqlParameter("@Radio19", supervisorscore.Radio19), new SqlParameter("@Radio20", supervisorscore.Radio20), new SqlParameter("@Creative", supervisorscore.Creative), new SqlParameter("@Reform", supervisorscore.Reform), new SqlParameter("@StuNum", supervisorscore.StuNum), new SqlParameter("@SupervisorScore", supervisorscore.SupervisorScore.ToString()), new SqlParameter("@Cntnt", "NA"), new SqlParameter("@SSID", supervisorscore.SSID) }; return db.ExecuteNonQuery(sql, parameters); }
// 罪魁祸首是下面这个变量 IsPostBack
ExecuteNonQuery返回了1,说明Update语句已经在SQL Server中执行成功了。
建议用SQL Server Profiler跟踪一下实际执行的Update语句。
正在用远程桌面,现在网速卡死,等晚点再试。谢谢dudu大神!
我查看SQL Server Profiler的结果,结果显示Update语句已经执行,但是字段没有值,textbox里输入了值,没有赋给Update语句的字段。好奇怪!
exec sp_executesql N'UPDATE [supervisorscore] SET [WorkID]=@WorkID, [SchoolYear]=@SchoolYear, [Semester]=@Semester, [Appliant]=@Appliant, [ApplicationDate]=@ApplicationDate, [Department]=@Department, [CourseName]=@CourseName, [Teacher]=@Teacher, [Class]=@Class, [TeachingRoom]=@TeachingRoom, [TeachingWeek]=@TeachingWeek, [TeachingDay]=@TeachingDay, [TeachingSection]=@TeachingSection, [Radio01]=@Radio01, [Radio02]=@Radio02, [Radio03]=@Radio03, [Radio04]=@Radio04, [Radio05]=@Radio05, [Radio06]=@Radio06, [Radio07]=@Radio07, [Radio08]=@Radio08, [Radio09]=@Radio09, [Radio10]=@Radio10, [Radio11]=@Radio11, [Radio12]=@Radio12, [Radio13]=@Radio13, [Radio14]=@Radio14, [Radio15]=@Radio15, [Radio16]=@Radio16, [Radio17]=@Radio17, [Radio18]=@Radio18, [Radio19]=@Radio19, [Radio20]=@Radio20, [Creative]=@Creative, [Reform]=@Reform, [StuNum]=@StuNum, [SupervisorScore]=@SupervisorScore, [Cntnt]=@Cntnt WHERE [SSID]=@SSID', N'@WorkID nvarchar(4),@SchoolYear nvarchar(4000),@Semester nvarchar(4000),@Appliant nvarchar(4000),@ApplicationDate nvarchar(4000),@Department nvarchar(4000),@CourseName nvarchar(4),@Teacher nvarchar(3),@Class nvarchar(4000),@TeachingRoom nvarchar(4000),@TeachingWeek nvarchar(4000),@TeachingDay nvarchar(4000),@TeachingSection nvarchar(4000),@Radio01 nvarchar(1),@Radio02 nvarchar(1),@Radio03 nvarchar(1),@Radio04 nvarchar(1),@Radio05 nvarchar(1),@Radio06 nvarchar(1),@Radio07 nvarchar(1),@Radio08 nvarchar(1),@Radio09 nvarchar(2),@Radio10 nvarchar(1),@Radio11 nvarchar(1),@Radio12 nvarchar(1),@Radio13 nvarchar(1),@Radio14 nvarchar(1),@Radio15 nvarchar(1),@Radio16 nvarchar(1),@Radio17 nvarchar(1),@Radio18 nvarchar(1),@Radio19 nvarchar(1),@Radio20 nvarchar(1),@Creative nvarchar(1),@Reform nvarchar(1),@StuNum nvarchar(1),@SupervisorScore nvarchar(2),@Cntnt nvarchar(2),@SSID nvarchar(11)',@WorkID=N'测试工号',@SchoolYear=N'',@Semester=N'',@Appliant=N'',@ApplicationDate=N'',@Department=N'',@CourseName=N'木材加工',@Teacher=N'张三丰',@Class=N'',@TeachingRoom=N'',@TeachingWeek=N'',@TeachingDay=N'',@TeachingSection=N'',@Radio01=N'3',@Radio02=N'5',@Radio03=N'3',@Radio04=N'4',@Radio05=N'2',@Radio06=N'2',@Radio07=N'2',@Radio08=N'4',@Radio09=N'10',@Radio10=N'5',@Radio11=N'4',@Radio12=N'4',@Radio13=N'4',@Radio14=N'4',@Radio15=N'4',@Radio16=N'5',@Radio17=N'5',@Radio18=N'3',@Radio19=N'3',@Radio20=N'5',@Creative=N'5',@Reform=N'5',@StuNum=N'2',@SupervisorScore=N'93',@Cntnt=N'NA',@SSID=N'20150801388'
@帕特里克: 哪个字段的值没被更新
先确定你提交的数据后台接收到时是不是已经更改了,如果收到的确实是修改后的数据,那你就要确认一下你是不是看错数据库或数据表了~~~
dudu大神说的对,用SQL Server的SQL Server Profiler没必要远程桌面呀,可以远程桌面,说明你有权限,直接用数据库工具连接上,域用户过滤为你自己的,看看你的语句执行的是什么就可以了