本文介绍了在datareader中从'DateTime'到'Int32'的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 hi 当我尝试读取datetime列sqldatareader $ b时,i从'DateTime'到'Int32'获取无效演员表$ b我的代码是 private ReferralDemographicsEntity GetDataFromReader(SqlDataReader sdrReader) { ReferralDemographicsEntity entity = new ReferralDemographicsEntity { ID = Convert.ToInt32( 0 + sdrReader [ ReferralID]), DateOfReferral = sdrReader.IsDBNull(sdrReader.GetOrdinal( DateOfReferral))? :Convert.ToDateTime(sdrReader [ DateOfReferral])。ToString( MM / dd / yyyy), UrgencyOfReferralID = sdrReader.IsDBNull(sdrReader.GetOrdinal( UrgencyOfReferralID ))? 0 :Convert.ToInt32(sdrReader [ DateOfReferral]), ReferralStatusID = sdrReader.IsDBNull(sdrReader.GetOrdinal( ReferralStatusID ))? 0 :Convert.ToInt32(sdrReader [ ReferralStatusID]), SSN = sdrReader [ SSN]。ToString() ?? , DateOfBirth = sdrReader.IsDBNull(sdrReader.GetOrdinal( DateOfBirth))? :Convert.ToDateTime(sdrReader [ DateOfBirth])。ToString( MM / dd / yyyy),年龄= sdrReader.IsDBNull(sdrReader.GetOrdinal( 年龄))? 0 :Convert.ToInt32(sdrReader [ 年龄]), PhoneType1ID = sdrReader.IsDBNull(sdrReader.GetOrdinal( PhoneType1ID))? 0 :Convert.ToInt32(sdrReader [ PhoneType1ID]), Phone1 = sdrReader [ Phone1]。ToString() ?? , PhoneNotes1 = sdrReader [ PhoneNotes1]。ToString()?? , PhoneType2ID = sdrReader.IsDBNull(sdrReader.GetOrdinal( PhoneType2ID))? 0 :Convert.ToInt32(sdrReader [ PhoneType2ID]), CreatedDate = Convert.ToDateTime(sdrReader [ CreatedDate]), ModifiedBy = Convert.ToInt32(sdrReader [ LastModifiedBy]), ModifiedDate = Convert.ToDateTime(sdrReader [ LastModifiedDate] ), IsActive = Convert.ToBoolean(sdrReader [ IsActive]), TimeStamp = Convert.ToInt32( 0 + sdrReader [ TimeStamp]), ImageFileName = sdrReader [ ImageFileName], UrgencyOfReferral = sdrReader [ UrgencyOfReferral]。ToString()?? , ReferralStatus = sdrReader [ ReferralStatus]。ToString()?? , PhoneType1 = sdrReader [ PhoneType1]。ToString()?? , PhoneType2 = sdrReader [ PhoneType2]。ToString()?? , Gender = sdrReader [ 性别]。ToString()?? , Language = sdrReader [ 语言]。ToString()?? }; 在我的数据库中所有日期时间列都存储了正确的日期格式,如2013-09-01 00:00:00.000 关于此问题的Plz帮助 谢谢 Manohar 解决方案 我想带下划线的行正在产生问题。 ReferralDemographicsEntity entity = new ReferralDemographicsEntity { ID = Convert.ToInt32( 0 + sdrReader [ ReferralID]), DateOfReferral = sdrReader.IsDBNull(sdrReader.GetOrdinal( DateOfReferral))? :Convert.ToDateTime(sdrReader [ DateOfReferral])。ToString( MM / dd / yyyy), UrgencyOfReferralID = sdrReader.IsDBNull(sdrReader.GetOrdinal( UrgencyOfReferralID))? 0 : Convert.ToInt32(sdrReader [ DateOfReferral] ), 它应该像... ReferralDemographicsEntity entity = new ReferralDemographicsEntity { ID = Convert.ToInt32( 0 + sdrReader [ ReferralID]), DateOfReferral = sdrReader.IsDBNull(sdrReader.GetOrdinal( DateOfReferral))? :Convert.ToDateTime(sdrReader [ DateOfReferral])。ToString( MM / dd / yyyy), UrgencyOfReferralID = sdrReader.IsDBNull(sdrReader.GetOrdinal( UrgencyOfReferralID))? 0 : Convert.ToInt32(sdrReader.GetOrdinal( UrgencyOfReferralID)。的ToString())日 hii get the Invalid cast from 'DateTime' to 'Int32' when i try to read datetime column sqldatareadermy code isprivate ReferralDemographicsEntity GetDataFromReader(SqlDataReader sdrReader){ ReferralDemographicsEntity entity = new ReferralDemographicsEntity { ID = Convert.ToInt32("0" + sdrReader["ReferralID"]), DateOfReferral = sdrReader.IsDBNull(sdrReader.GetOrdinal("DateOfReferral")) ? "" : Convert.ToDateTime(sdrReader["DateOfReferral"]).ToString("MM/dd/yyyy"), UrgencyOfReferralID = sdrReader.IsDBNull(sdrReader.GetOrdinal("UrgencyOfReferralID")) ? 0 : Convert.ToInt32(sdrReader["DateOfReferral"]), ReferralStatusID = sdrReader.IsDBNull(sdrReader.GetOrdinal("ReferralStatusID")) ? 0 : Convert.ToInt32(sdrReader["ReferralStatusID"]), SSN = sdrReader["SSN"].ToString() ?? "", DateOfBirth = sdrReader.IsDBNull(sdrReader.GetOrdinal("DateOfBirth")) ? "" : Convert.ToDateTime(sdrReader["DateOfBirth"]).ToString("MM/dd/yyyy"), Age = sdrReader.IsDBNull(sdrReader.GetOrdinal("Age")) ? 0 : Convert.ToInt32(sdrReader["Age"]), PhoneType1ID = sdrReader.IsDBNull(sdrReader.GetOrdinal("PhoneType1ID")) ? 0 : Convert.ToInt32(sdrReader["PhoneType1ID"]), Phone1 = sdrReader["Phone1"].ToString() ?? "", PhoneNotes1 = sdrReader["PhoneNotes1"].ToString() ?? "", PhoneType2ID = sdrReader.IsDBNull(sdrReader.GetOrdinal("PhoneType2ID")) ? 0 : Convert.ToInt32(sdrReader["PhoneType2ID"]), CreatedDate = Convert.ToDateTime(sdrReader["CreatedDate"]), ModifiedBy = Convert.ToInt32(sdrReader["LastModifiedBy"]), ModifiedDate = Convert.ToDateTime(sdrReader["LastModifiedDate"]), IsActive = Convert.ToBoolean(sdrReader["IsActive"]), TimeStamp = Convert.ToInt32("0" + sdrReader["TimeStamp"]), ImageFileName = sdrReader["ImageFileName"], UrgencyOfReferral = sdrReader["UrgencyOfReferral"].ToString() ?? "", ReferralStatus = sdrReader["ReferralStatus"].ToString() ?? "", PhoneType1 = sdrReader["PhoneType1"].ToString() ?? "", PhoneType2 = sdrReader["PhoneType2"].ToString() ?? "", Gender = sdrReader["Gender"].ToString() ?? "", Language = sdrReader["Language"].ToString() ?? "" };in my database all datetime column is storing correct dateformat like "2013-09-01 00:00:00.000"Plz help regarding this issueThanksManohar 解决方案 I guess the underlined line is creating the problem.ReferralDemographicsEntity entity = new ReferralDemographicsEntity { ID = Convert.ToInt32("0" + sdrReader["ReferralID"]), DateOfReferral = sdrReader.IsDBNull(sdrReader.GetOrdinal("DateOfReferral")) ? "" : Convert.ToDateTime(sdrReader["DateOfReferral"]).ToString("MM/dd/yyyy"), UrgencyOfReferralID = sdrReader.IsDBNull(sdrReader.GetOrdinal("UrgencyOfReferralID")) ? 0 : Convert.ToInt32(sdrReader["DateOfReferral"]),It should be like...ReferralDemographicsEntity entity = new ReferralDemographicsEntity { ID = Convert.ToInt32("0" + sdrReader["ReferralID"]), DateOfReferral = sdrReader.IsDBNull(sdrReader.GetOrdinal("DateOfReferral")) ? "" : Convert.ToDateTime(sdrReader["DateOfReferral"]).ToString("MM/dd/yyyy"), UrgencyOfReferralID = sdrReader.IsDBNull(sdrReader.GetOrdinal("UrgencyOfReferralID")) ? 0 : Convert.ToInt32(sdrReader.GetOrdinal("UrgencyOfReferralID").ToString()), 这篇关于在datareader中从'DateTime'到'Int32'的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-16 01:00