日期:2014-05-18  浏览次数:20843 次

急,急,更新计算问题??
//更新
  pp.ObjectAge = Age(this.txtIdCard.Text); txtIdCard为从文本框接收的身份证号码
  pp.UpdateObjectInformation();

  private int Age(string txtIdCard)
  {
  DateTime birthday = BirthDate(this.txtIdCard.Text);
  //return DateTime.Today.CompareTo(birthday);
  return DateTime.Today.Year - birthday.Year;
  }

 private DateTime BirthDate(string txtIdCard)
  {
  try
  {
  if (txtIdCard.Length == 15)
  {
  return new DateTime(int.Parse("19" + txtIdCard.Substring(7, 2)), int.Parse(txtIdCard.Substring(9, 2)), int.Parse(txtIdCard.Substring(11, 2)));
  }
  else if (txtIdCard.Length == 18)
  {
  return new DateTime(int.Parse(txtIdCard.Substring(6, 4)), int.Parse(txtIdCard.Substring(10, 2)), int.Parse(txtIdCard.Substring(12, 2)));
  }
  else
  return DateTime.MinValue;
  }
  catch { return DateTime.MinValue; }
  }

  public void UpdateObjectInformation(int oid) 
  {
  string sql = "UPDATE [dbo].[C_Work_Object] Set " +  
  "[Obj_age]='" + _age +
  "',[Obj_optime]=" +
  "getdate() where Obj_id=" + oid;
  this.dbProcessor.ExecuteSql(sql);
  }

当在文本框添加身份证号码时,可以计算出年龄,但当修改身份证号码时,年龄变成了0;请问哪里有错误啊??望指教??

------解决方案--------------------
BirthDate(this.txtIdCard.Text.Trim()); 试一下
------解决方案--------------------
if (txtIdCard.Length == 15)
{
return new DateTime(int.Parse("19" + txtIdCard.Substring(6, 2)), int.Parse(txtIdCard.Substring(8, 2)), int.Parse(txtIdCard.Substring(10, 2)));
}

十五位的不对 出现异常了