将varchar数据类型转换为日期时间数据类型会导致超出范围的值。该语句已终止

时间:2014-03-20 14:27:55

标签: c# sql sql-server web-services webmethod

尝试在数据库中保存注册日期时出现以下错误。将varchar数据类型转换为日期时间数据类型会导致超出范围的值。声明已经终止。

[WebMethod]
        public bool Attendance(Attendance attendance)
        {
            bool UploadSuccess = false;
            cn.Open();

            SqlCommand com = new SqlCommand("INSERT INTO tblAttendance (StudentID, RegistrationDate) values (" +
                attendance.StudentID + " ,'" +
                attendance.RegistrationDate + "')", cn);
              {
                com.Parameters.AddWithValue("@StudentID", attendance.StudentID);
                com.Parameters.AddWithValue("@RegistrationDate", attendance.RegistrationDate);
                int i = com.ExecuteNonQuery();
                cn.Close();
                if (i != 0)
                {
                    UploadSuccess = true;
                }

                return UploadSuccess;
            }
        }

我想在学生成功注册后保存当前日期

  Attendance record = new Attendance();
 record.StudentID = a.StudentID;
  record.RegistrationDate = DateTime.Now;

0 个答案:

没有答案