获取 "the conversion of a varchar data type to a datetime data type resulted in an out-of-range value" 即使没有日期时间列

Getting "the conversion of a varchar data type to a datetime data type resulted in an out-of-range value" even if there is no datetime column

我的 SQL 服务器数据库中有一个过程,其中 returns 记录集中的一些日期,我在其中获取日期的列的数据类型为 varchar。我正在使用 LINQ2SQL 来执行该过程。

问题是,当我在 SSMS 中执行该过程时,它可以完美无误地执行,但是当我通过我的应用程序执行它时,它会给出

the conversion of a varchar data type to a datetime data type resulted in an out-of-range value

错误。我还检查了由 LINQ2SQL class 构建器为该过程的记录集生成的 class 也不包含任何具有数据类型 datetime 的 属性 ].

该程序太长,无法分享,但我正在分享该程序的最终 SELECT,该程序由 LINQ2SQL class 生成器检测到,以生成 class.在这里

DECLARE @finalResultSetTempTable AS TABLE ( 
                                              cd_id               BIGINT NOT NULL , 
                                              service_id          INT NULL , 
                                              resident_id         VARCHAR(20) , 
                                              cd_Date             VARCHAR(20) , 
                                              cd_StartTime        VARCHAR(20) , 
                                              cd_EndTime          VARCHAR(20) , 
                                              Pre_travelTime      VARCHAR(20) , 
                                              Post_travelTime     VARCHAR(20) , 
                                              cd_TimeDuration     VARCHAR(20) , 
                                              UnitType            INT , 
                                              NoOfUnits           INT , 
                                              cd_IsCompleted      BIT , 
                                              AssignedTo          INT , 
                                              cd_status           VARCHAR(50) , 
                                              Service_name        VARCHAR(200) , 
                                              Application_Name    VARCHAR(50) , 
                                              emp_first_name      VARCHAR(200) , 
                                              emp_last_name       VARCHAR(200) , 
                                              resident_first_name VARCHAR(200) , 
                                              resident_last_name  VARCHAR(200) , 
                                              address             VARCHAR(200) , 
                                              suburb              VARCHAR(200) , 
                                              state               VARCHAR(200) , 
                                              postCode            VARCHAR(200)
                                              );

SELECT cd_id , service_id , resident_id , FORMAT(CAST(cd_Date AS DATETIME) , 'yyyy-MM-dd') AS cd_Date , ISNULL(cd_StartTime , '12:00 AM') AS cd_StartTime , ISNULL(cd_EndTime , '12:00 AM') AS cd_EndTime , Pre_travelTime , Post_travelTime , ISNULL(cd_TimeDuration , '0') AS cd_TimeDuration , UnitType , NoOfUnits , cd_IsCompleted , AssignedTo , cd_status , Service_name , emp_first_name , emp_last_name , resident_first_name , resident_last_name , address , suburb , state , postCode , Application_Name
            FROM @finalResultSetTempTable

LINQ2SQL生成的class如下

public partial class sp_GetServicesForBulkUpdateResult
{

    private long _cd_id;

    private System.Nullable<int> _service_id;

    private string _resident_id;

    private string _cd_Date;

    private string _cd_StartTime;

    private string _cd_EndTime;

    private string _Pre_travelTime;

    private string _Post_travelTime;

    private string _cd_TimeDuration;

    private System.Nullable<int> _UnitType;

    private System.Nullable<int> _NoOfUnits;

    private System.Nullable<bool> _cd_IsCompleted;

    private System.Nullable<int> _AssignedTo;

    private string _cd_status;

    private string _Service_name;

    private string _emp_first_name;

    private string _emp_last_name;

    private string _resident_first_name;

    private string _resident_last_name;

    private string _address;

    private string _suburb;

    private string _state;

    private string _postCode;

    private string _Application_Name;

    public sp_GetServicesForBulkUpdateResult()
    {
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_id", DbType="BigInt NOT NULL")]
    public long cd_id
    {
        get
        {
            return this._cd_id;
        }
        set
        {
            if ((this._cd_id != value))
            {
                this._cd_id = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_service_id", DbType="Int")]
    public System.Nullable<int> service_id
    {
        get
        {
            return this._service_id;
        }
        set
        {
            if ((this._service_id != value))
            {
                this._service_id = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_resident_id", DbType="VarChar(20)")]
    public string resident_id
    {
        get
        {
            return this._resident_id;
        }
        set
        {
            if ((this._resident_id != value))
            {
                this._resident_id = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_Date", DbType="NVarChar(4000)")]
    public string cd_Date
    {
        get
        {
            return this._cd_Date;
        }
        set
        {
            if ((this._cd_Date != value))
            {
                this._cd_Date = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_StartTime", DbType="VarChar(20) NOT NULL", CanBeNull=false)]
    public string cd_StartTime
    {
        get
        {
            return this._cd_StartTime;
        }
        set
        {
            if ((this._cd_StartTime != value))
            {
                this._cd_StartTime = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_EndTime", DbType="VarChar(20) NOT NULL", CanBeNull=false)]
    public string cd_EndTime
    {
        get
        {
            return this._cd_EndTime;
        }
        set
        {
            if ((this._cd_EndTime != value))
            {
                this._cd_EndTime = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Pre_travelTime", DbType="VarChar(20)")]
    public string Pre_travelTime
    {
        get
        {
            return this._Pre_travelTime;
        }
        set
        {
            if ((this._Pre_travelTime != value))
            {
                this._Pre_travelTime = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Post_travelTime", DbType="VarChar(20)")]
    public string Post_travelTime
    {
        get
        {
            return this._Post_travelTime;
        }
        set
        {
            if ((this._Post_travelTime != value))
            {
                this._Post_travelTime = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_TimeDuration", DbType="VarChar(20) NOT NULL", CanBeNull=false)]
    public string cd_TimeDuration
    {
        get
        {
            return this._cd_TimeDuration;
        }
        set
        {
            if ((this._cd_TimeDuration != value))
            {
                this._cd_TimeDuration = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_UnitType", DbType="Int")]
    public System.Nullable<int> UnitType
    {
        get
        {
            return this._UnitType;
        }
        set
        {
            if ((this._UnitType != value))
            {
                this._UnitType = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NoOfUnits", DbType="Int")]
    public System.Nullable<int> NoOfUnits
    {
        get
        {
            return this._NoOfUnits;
        }
        set
        {
            if ((this._NoOfUnits != value))
            {
                this._NoOfUnits = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_IsCompleted", DbType="Bit")]
    public System.Nullable<bool> cd_IsCompleted
    {
        get
        {
            return this._cd_IsCompleted;
        }
        set
        {
            if ((this._cd_IsCompleted != value))
            {
                this._cd_IsCompleted = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AssignedTo", DbType="Int")]
    public System.Nullable<int> AssignedTo
    {
        get
        {
            return this._AssignedTo;
        }
        set
        {
            if ((this._AssignedTo != value))
            {
                this._AssignedTo = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cd_status", DbType="VarChar(50)")]
    public string cd_status
    {
        get
        {
            return this._cd_status;
        }
        set
        {
            if ((this._cd_status != value))
            {
                this._cd_status = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Service_name", DbType="VarChar(200)")]
    public string Service_name
    {
        get
        {
            return this._Service_name;
        }
        set
        {
            if ((this._Service_name != value))
            {
                this._Service_name = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_emp_first_name", DbType="VarChar(200)")]
    public string emp_first_name
    {
        get
        {
            return this._emp_first_name;
        }
        set
        {
            if ((this._emp_first_name != value))
            {
                this._emp_first_name = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_emp_last_name", DbType="VarChar(200)")]
    public string emp_last_name
    {
        get
        {
            return this._emp_last_name;
        }
        set
        {
            if ((this._emp_last_name != value))
            {
                this._emp_last_name = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_resident_first_name", DbType="VarChar(200)")]
    public string resident_first_name
    {
        get
        {
            return this._resident_first_name;
        }
        set
        {
            if ((this._resident_first_name != value))
            {
                this._resident_first_name = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_resident_last_name", DbType="VarChar(200)")]
    public string resident_last_name
    {
        get
        {
            return this._resident_last_name;
        }
        set
        {
            if ((this._resident_last_name != value))
            {
                this._resident_last_name = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_address", DbType="VarChar(200)")]
    public string address
    {
        get
        {
            return this._address;
        }
        set
        {
            if ((this._address != value))
            {
                this._address = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_suburb", DbType="VarChar(200)")]
    public string suburb
    {
        get
        {
            return this._suburb;
        }
        set
        {
            if ((this._suburb != value))
            {
                this._suburb = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_state", DbType="VarChar(200)")]
    public string state
    {
        get
        {
            return this._state;
        }
        set
        {
            if ((this._state != value))
            {
                this._state = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_postCode", DbType="VarChar(200)")]
    public string postCode
    {
        get
        {
            return this._postCode;
        }
        set
        {
            if ((this._postCode != value))
            {
                this._postCode = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Application_Name", DbType="VarChar(50)")]
    public string Application_Name
    {
        get
        {
            return this._Application_Name;
        }
        set
        {
            if ((this._Application_Name != value))
            {
                this._Application_Name = value;
            }
        }
    }
}

我编写的执行程序的代码如下

Service service;
                res = new List<Service>();
                var resultSet = context.sp_GetServicesForBulkUpdate(assignedTo, _fromDate, _toDate, programName, serviceID.ToString(), "ALL");

                foreach (var item in resultSet)
                {
                    service = new Service();
                    service.serviceDeliveryID = item.cd_id.ToString();
                    service.serviceID = item.service_id.ToString();
                    service.serviceStatus = item.cd_status;
                    service.serviceName = item.Service_name;
                    service.serviceStartDate = string.IsNullOrEmpty(item.cd_Date) ? "" : item.cd_Date;
                    service.serviceStartTime = item.cd_StartTime;
                    service.serviceEndDate = string.IsNullOrEmpty(item.cd_Date) ? "" : item.cd_Date;
                    service.serviceEndTime = item.cd_EndTime;
                    service.serviceLocation = ((string.IsNullOrEmpty(item.address) ? "" : item.address + ", ") + (string.IsNullOrEmpty(item.suburb) ? "" : item.suburb + ", ") + (string.IsNullOrEmpty(item.state) ? "" : item.state + ", ") + (string.IsNullOrEmpty(item.postCode) ? "" : item.postCode + " ")).Trim().TrimEnd(',');

                    service.serviceUnitType = item.UnitType;
                    service.serviceNoOfUnits = item.NoOfUnits;

                    service.employeeID = Int32.Parse((item.AssignedTo.HasValue ? item.AssignedTo.ToString() : "0"));
                    service.employeeFirstName = string.IsNullOrEmpty(item.emp_first_name) ? "" : item.emp_first_name;
                    service.employeeLastName = string.IsNullOrEmpty(item.emp_last_name) ? "" : item.emp_last_name;

                    service.residentID = item.resident_id;
                    service.residentFirstName = item.resident_first_name;
                    service.residentLastName = item.resident_last_name;

                    service.isAssigned = (item.AssignedTo.HasValue && item.AssignedTo != 0) ? true : false;

                    service.isActive = item.cd_IsCompleted.ToString() == "0" ? false : true;
                    service.serviceAppName = item.Application_Name;


                    res.Add(service);
                }

这里是程序返回的一些示例数据,如果有帮助的话

任何人都可以提供任何帮助吗?

编辑

由于 excel sheet,所附屏幕截图中的日期格式为 m/dd/yyyy。实际格式是yyyy-mm-dd

DECLARE @finalResultSetTempTable AS TABLE ( 
                                              cd_id               BIGINT NOT NULL , 
                                              service_id          INT NULL , 
                                              resident_id         VARCHAR(20) , 
                                              cd_Date             VARCHAR(20) , 
                                              cd_StartTime        VARCHAR(20) , 
                                              cd_EndTime          VARCHAR(20) , 
                                              Pre_travelTime      VARCHAR(20) , 
                                              Post_travelTime     VARCHAR(20) , 
                                              cd_TimeDuration     VARCHAR(20) , 
                                              UnitType            INT , 
                                              NoOfUnits           INT , 
                                              cd_IsCompleted      BIT , 
                                              AssignedTo          INT , 
                                              cd_status           VARCHAR(50) , 
                                              Service_name        VARCHAR(200) , 
                                              Application_Name    VARCHAR(50) , 
                                              emp_first_name      VARCHAR(200) , 
                                              emp_last_name       VARCHAR(200) , 
                                              resident_first_name VARCHAR(200) , 
                                              resident_last_name  VARCHAR(200) , 
                                              address             VARCHAR(200) , 
                                              suburb              VARCHAR(200) , 
                                              state               VARCHAR(200) , 
                                              postCode            VARCHAR(200)
                                              );

SELECT cd_id , service_id , resident_id ,  CONVERT(datetime,cd_Date,103) AS cd_Date , ISNULL(cd_StartTime , '12:00 AM') AS cd_StartTime , ISNULL(cd_EndTime , '12:00 AM') AS cd_EndTime , Pre_travelTime , Post_travelTime , ISNULL(cd_TimeDuration , '0') AS cd_TimeDuration , UnitType , NoOfUnits , cd_IsCompleted , AssignedTo , cd_status , Service_name , emp_first_name , emp_last_name , resident_first_name , resident_last_name , address , suburb , state , postCode , Application_Name
            FROM @finalResultSetTempTable

调查程序后,我发现 FORMAT(CAST(cd_Date AS DATETIME) , 'yyyy-MM-dd') AS cd_Date 造成了问题。因为我的数据已经是那种格式了,当 CAST(cd_Date AS DATETIME) 试图转换 datetime 中的 varchar 值时,它给出了错误。

根据我的说法,CAST(cd_Date AS DATETIME) 需要 varchar 格式的 yyyy-dd-MM 日期,因为我用来查询数据库的用户将其语言设置为英国语(我从this question) ,因此当它像 2015-05-16 (采用 yyyy-MM-dd 格式)那样转换日期时,它会给出错误

the conversion of a varchar data type to a datetime data type resulted in an out-of-range value

如有错误请指正,如有遗漏请补充。