日期:2014-05-17  浏览次数:20426 次

不能看懂代码,谁能逐行帮我解释一下~
public Model.Article GetModel(int ID)
  {
  StringBuilder strSql = new StringBuilder();
  strSql.Append("select ");
  strSql.Append(" ID,Author,TypeId,Subject,Content,[Count],date ");
  strSql.Append(" from Article ");
  strSql.Append(" where ID=" + ID + " ");
  using (IDataReader dataReader = SQLHelper.ExecuteReader(strSql.ToString()))
  {
  if (dataReader.Read())
  {
  model.ID = ID;
  model.Author = dataReader["Author"].ToString();
  model.TypeId = Convert.ToInt32(dataReader["TypeId"]);
  model.Subject = dataReader["Subject"].ToString();
  model.Content = dataReader["Content"].ToString();
  model.Count = Convert.ToInt32(dataReader["Count"]);
  model.date = Convert.ToDateTime(dataReader["Date"]);
  }
  }
  return model;
  }

------解决方案--------------------
//查询方法,实体类为Article,传入条件参数ID
C# code
public Model.Article GetModel(int ID)
  {
  StringBuilder strSql = new StringBuilder();//实例化StringBuilder,对象为strSql
  strSql.Append("select ");//从此处开始为追查字符串的查询
  strSql.Append(" ID,Author,TypeId,Subject,Content,[Count],date ");
  strSql.Append(" from Article ");
  strSql.Append(" where ID=" + ID + " ");//查询到此结束
  using (IDataReader dataReader = SQLHelper.ExecuteReader(strSql.ToString()))//在此释放IDataReader对象,并将对象strSql作为参数传入到此处
  {
  if (dataReader.Read())//开始读取数据
  {
  model.ID = ID;//因为ID作为查询条件已经在方法中声明,故而再次不用像下面方式
  model.Author = dataReader["Author"].ToString();//读取数据库字段值并将该值赋给实体类对象
  model.TypeId = Convert.ToInt32(dataReader["TypeId"]);//同上
  model.Subject = dataReader["Subject"].ToString();
  model.Content = dataReader["Content"].ToString();
  model.Count = Convert.ToInt32(dataReader["Count"]);
  model.date = Convert.ToDateTime(dataReader["Date"]);
  }
  }
  return model;//返回该实体对象
  }