c# – 无效的投射日期时间?
发布时间:2020-12-16 00:19:49 所属栏目:百科 来源:网络整理
导读:我有一个包含以下代码的类 public cCase(string pCaseNo,string pMode) { if (pMode == "new") { this._caseNo = Validate_CaseNo(pCaseNo); } if (pMode == "existing") { try { int intValidatedCaseNo = Validate_CaseNo(pCaseNo); string sqlText = "SEL
我有一个包含以下代码的类
public cCase(string pCaseNo,string pMode) { if (pMode == "new") { this._caseNo = Validate_CaseNo(pCaseNo); } if (pMode == "existing") { try { int intValidatedCaseNo = Validate_CaseNo(pCaseNo); string sqlText = "SELECT * FROM tblCases WHERE CaseNo = @CaseNo;"; string strConnection = cConnectionString.BuildConnectionString(); SqlConnection linkToDB = new SqlConnection(strConnection); linkToDB.Open(); SqlCommand sqlCom = new SqlCommand(sqlText,linkToDB); sqlCom.Parameters.Add("@CaseNo",SqlDbType.Int); sqlCom.Parameters["@CaseNo"].Value = intValidatedCaseNo; SqlDataReader caseReader = sqlCom.ExecuteReader(); if (caseReader.HasRows) while (caseReader.Read()) { this._claimant = caseReader["Claimant"].ToString(); this._defendant = caseReader["Defendant"].ToString(); this._caseType = caseReader["CaseType"].ToString(); this._occupation = caseReader["Occupation"].ToString(); this._doa = (DateTime?)caseReader["DOA"]; this._dateClosed = (DateTime?)caseReader["DateClosed"]; this._dateSettled = (DateTime?)caseReader["DateSettled"]; this._dateInstructed = (DateTime?)caseReader["DateInstructed"]; this._status = caseReader["Status"].ToString(); this._instructionType = caseReader["InstructionType"].ToString(); this._feeEstimate = (decimal?)caseReader["FeeEstimate"]; this._amountClaimed = (decimal?)caseReader["AmountClaimed"]; this._amountSettled = (decimal?)caseReader["AmountSettled"]; this._caseManager = caseReader["CaseManager"].ToString(); } caseReader.Close(); linkToDB.Close(); linkToDB.Dispose(); } catch (Exception eX) { throw new Exception("Error finding case" + Environment.NewLine + eX.Message); } } } 但是Datetime?强制转换失败并显示“无效投射”. 请帮忙. 解决方法
您将要更改以下行:
this._doa = (DateTime?)caseReader["DOA"]; 至: if (caseReader["DOA"] != DBNull.Value) this._doa.Value = (DateTime)caseReader["DOA"]; 以及所有类似的线条. DBNull值无法从Nullable类型中转换. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |