Spurs_lipu 2017-03-30 08:19 采纳率: 0%
浏览 3018
已结题

从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值。

public DataTable GetStudentBetween(string StartTime , string EndTime)
{
StringBuilder strSql = new StringBuilder();
strSql.AppendLine(" Select A.id,A.createdate,A.lesson_plan_id,A.userid,A.useflg,A.delflg,A.qdflg ,U.num,U.username,U.username AS username2 ");
strSql.AppendLine(" ,case A.qdflg when 0 then '未到' when 1 then '已到' else '' end as qiandao ");
strSql.AppendLine(" ,1 as Sort ");
strSql.AppendLine(" From tbl_lesson_student AS A ");
strSql.AppendLine(" Left join tbl_lesson_plan AS B ");
strSql.AppendLine(" ON A.lesson_plan_id=B.id ");
strSql.AppendLine(" Left join tbl_user U ");
strSql.AppendLine(" ON A.userid =U.userid ");
if (StartTime.Trim() != "" && EndTime.Trim() != "")
{
strSql.AppendLine(" Where A.createdate between '" + StartTime + "'and '" + EndTime + "'");
}
strSql.AppendLine(" Union ");
strSql.AppendLine(" Select A.id,A.createdate,A.lesson_plan_id,A.userid,A.useflg,A.delflg,A.qdflg ,100,U.expname AS username,U.expname +'('+convert(VARCHAR,Round(convert(float,DATEDIFF(m,U.birthday,getdate()))/12,1))+':'+G.username +')' AS username2 ");
strSql.AppendLine(" ,case A.qdflg when 0 then '未到' when 1 then '已到' else '' end as qiandao ");
strSql.AppendLine(" ,2 as Sort ");
strSql.AppendLine(" From tbl_lesson_exp AS A ");
strSql.AppendLine(" Left join tbl_lesson_plan AS B ");
strSql.AppendLine(" ON A.lesson_plan_id=B.id ");
strSql.AppendLine(" Left join tbl_user_exp U ");
strSql.AppendLine(" ON A.userid =U.expid ");
strSql.AppendLine(" Left join tbl_user G ");
strSql.AppendLine(" ON G.userid=U.userid ");
if (StartTime.Trim() != "" && EndTime.Trim() != "")
{
strSql.AppendLine(" Where A.createdate between '" + StartTime + "'and '" + EndTime + "'");
}
strSql.AppendLine(" Order BY Sort, id ");

    return DbHelperSQL.Query(strSql.ToString()).Tables[0];
}
        在用C#访问数据库时代码如上会出现题目所示的错误,如果把StartTime EndTime这些改成具体的时间如:’2017-03-01’就没问题能通过。大神求解求解
  • 写回答

3条回答

  • 当故事结束以后 2017-03-30 08:25
    关注

    StartTime 加个.会弹出来类型。你看加个匹配类型是否管用。或者你调试看看StartTime里面是时间吗?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画