加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何修复“SqlException:将datetime2数据类型转换为日期

发布时间:2020-12-15 18:23:50 所属栏目:百科 来源:网络整理
导读:SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 我的代码是这样的: using (var contxt = new realtydbEntities()) { var status = GetStatus(); var repIssue = new RepairIssue() {

SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

我的代码是这样的:

using (var contxt = new realtydbEntities())
        {
            var status = GetStatus();

            var repIssue = new RepairIssue()
            {
                CreaterId = AuthorId,RepairItemDesc = this.txtDescription.Text,CreateDate = DateTime.Now,//here's the problem
                RepairIssueStatu = status
            };

            contxt.AddObject("RepairIssues",repIssue);
            contxt.SaveChanges();
        }

CreateDate属性映射到类型为smalldatetime的列.

如何让这段代码运行?

解决方法

您的问题的根源是C#DateTime对象比SQL的smalldatetime类型“更大”.以下是对差异的概述: http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

所以你的选择真的是:

>将列类型从smalldatetime更改为datetime(或datetime2)>而不是使用EF,构建自己的SQL命令(并且可以使用SqlDateTime)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读