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> 编号: </li>
<li>标题: </li>
<li>内容: </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);
}
用别的控件行吗?比如Label,把相应的属性值赋值给Label的Text
比如是否可以而类似于JSp,这样来用:
编号:<%=a.ArticleId%>
标题:<%=a.Title%>
我用了标签,但是页面没有显示:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Article a = new Article();
a = ArticleManager.GetArticlesByArticleId(200);
Label1.Text = a.ArticleId.ToString();
Label2.Text = a.ArticleDetails.ToString();
}
}
@WesternWind: 用Label可以啊,只要确保有值
@yanzhe:
用这个 返回一个Article对象可以显示啦
if (!IsPostBack)
{
Article a = new Article();
a = ArticleManager.GetArticlesByArticleId(200);
Literal1.Text = a.ArticleId.ToString();
Literal2.Text= a.ArticleDetails.ToString();
}
@yanzhe: 如果返回一个IList<Article>,该如何呈现呢 !?
<div>
<ul>
<li> 编号: </li>
<li>标题: </li>
<li>内容: </li>
………..
</ul>
</div>
@WesternWind: 那直接在前台有个Table,别用列表了,在后台填充Table就可以了。
List<Artical> lists = ArticleManager.GetAtriclesByArticleId(200);
for (int i = 0; i < lists.Count; i++)
{
TableRow row = new TableRow();
Table1.Rows.Add(row);
TableCell cell0 = new TableCell();
cell0.Text = lists[i].ArticleId.ToString();
TableCell cell1 = new TableCell();
cell1.Text = lists[i].ChannelId.ToString();
TableCell cell2 = new TableCell();
cell2.Text = lists[i].Title.ToString();
TableCell cell3 = new TableCell();
cell3.Text = lists[i].ArticleDetails.ToString();
row.Cells.Add(cell0);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
}
@WesternWind:
你写错了
if (Page.IsPostBack) 这是页面回传。
加个非 if (!Page.IsPostBack)
@WesternWind: 用了标签,还要在后台页面调用 Page.DataBind();绑定数据才行。。。