建模时候多写个Author或UserName的属性,BLL里连接表把值取出来就ok了。demo写得粗糙,见笑
Model:
public class News
{
public News()
{}
private int _id;
private int _uid;
private int _category1;
private int _category2;
private int _category3;
private string _title;
private string _author;
private string _source;
private string _content;
public int NewsId
{
set{ _id=value;}
get{return _id;}
}
public int UserId
{
set{ _uid=value;}
get{return _uid;}
}
/// <summary>
///
/// </summary>
public int Category1
{
set{ _category1=value;}
get{return _category1;}
}
/// <summary>
///
/// </summary>
public int Category2
{
set{ _category2=value;}
get{return _category2;}
}
/// <summary>
///
/// </summary>
public int Category3
{
set{ _category3=value;}
get{return _category3;}
}
/// <summary>
///
/// </summary>
public string Title
{
set{ _title=value;}
get{return _title;}
}
/// <summary>
///
/// </summary>
public string Author
{
set{ _author=value;}
get{return _author;}
}
/// <summary>
///
/// </summary>
public string Source
{
set{ _source=value;}
get{return _source;}
}
public string Content
{
set{ _content=value;}
get{return _content;}
}
BLL:
/// <summary>
/// 得到一个对象实体
/// </summary>
public Model.News GetModel(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select News.*,Users.UserName from News ");
strSql.Append("LEFT JOIN Users ON Users.UserId=News.UserId");
strSql.Append(" where NewsId=" + id);
Model.News model = new Model.News();
DataTable dt = SQLHelper.PopulateDataTable(strSql.ToString());
model.NewsId = id;
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["UserId"].ToString() != "")
{
model.UserId = int.Parse(dt.Rows[0]["UserId"].ToString());
}
if (dt.Rows[0]["Category1"].ToString() != "")
{
model.Category1 = int.Parse(dt.Rows[0]["Category1"].ToString());
}
if (dt.Rows[0]["Category2"].ToString() != "")
{
model.Category2 = int.Parse(dt.Rows[0]["Category2"].ToString());
}
if (dt.Rows[0]["Category3"].ToString() != "")
{
model.Category3 = int.Parse(dt.Rows[0]["Category3"].ToString());
}
model.Title = dt.Rows[0]["Title"].ToString();
model.Author = dt.Rows[0]["UserName"].ToString();
model.Source = dt.Rows[0]["Source"].ToString();
model.NewsPic = dt.Rows[0]["NewsPic"].ToString();
model.ExUrl = dt.Rows[0]["ExUrl"].ToString();
model.Content = dt.Rows[0]["Content"].ToString();
return model;
}
else
{
return null;
}
}
建议是直接照搬数据库结构,把ID号弄过来就行,Linq To SQL就是这么干的,要读取导航数据的话可以通过成员方法或扩展方法实现。
另外还是建议你用ORM框架,因为你这个归根到底也是在实现ORM的功能,而你自己写的不太可能比那些成熟的ORM框架更优秀,无论是通用性、易用性、可扩展性还是性能方面,既然如此,何必费力费时不得好呢。
这个确实很痛苦,如果搞不好,维护起来就是噩梦一场