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

不用ASP.Net的GrewView等控件,如何掉调用 业务逻辑层 。 把数据呈现在页面
1、业务实体:
  public class Article
  {
  private int _articleId;
private string _title;
  private string _articleDetails;  

  public int ArticleId
  {
  get { return _articleId; }
  set { _articleId = value; }
  }  

}
2、通用的SQLHelper 方法:
/// <summary>
  /// 执¡ä行D有®D参?SQL语®?句?,ê?并¡é返¤¦Ì回?SqlDataReader
  /// </summary>
  public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
  {
  SqlCommand cmd = new SqlCommand(sql, Connection);
  cmd.Parameters.AddRange(values);
  SqlDataReader reader = cmd.ExecuteReader();
  return reader;
  }

3、数据访问层方法:
数据填充方法:
  private static Article FillRecord(SqlDataReader reader)
  {
  Article a = new Article();

  a.ArticleId = Convert.ToInt32(reader["articleId"]);
  a.ChannelId = Convert.ToInt32(reader["channelId"]);
  a.Title = Convert.ToString(reader["title"]);
  a.ArticleDetails = Convert.ToString(reader["articleDetails"]);
  return a;  
  }
   
数据访问层代码:
public static Article GetArticleByArticleId(int id)
  {
  string sql = "select * from article where articleId=@articleId";
  try
  {
  using (SqlDataReader reader = SqlHelper.GetReader(sql, new SqlParameter("@articleId", id)))
  {
  if (reader.Read())
  { return FillRecord(reader); }
  else
  { return null; }
  }
  }
  catch (Exception e)
  {
  Console.WriteLine(e.Message); throw e;
  }
  }
5、业务逻辑方法:
public static class ArticleManager
  {
  public static Article GetArticlesByArticleId(int id)
  {
  return ArticleService.GetArticleByArticleId(id);
  }
}
5、页面表现层:

<div>
  <ul>
  <li> 编号:&nbsp; </li>
  <li>标题: &nbsp; </li>
  <li>内容: &nbsp; </li>
………..
  </ul>
  </div>
6、请问不用ASP.Net的GrewView等控件,如何掉调用 业务逻辑层 。 把数据呈现在页面
public partial class _Default : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {

  Article a = ArticleManager. GetArticlesByArticleId (1);
   


  }



------解决方案--------------------
<div>
<ul>
<li> 编号:<asp:Literal id=no runat=server/> </li>
<li>标题: &nbsp; </li>
<li>内容: &nbsp; </li>

</ul>
</div>

Article a = ArticleManager. GetArticlesByArticleId (1);
no.Text = a.ArticleId;