泛型与SqlDataReader
从数据库中直接导出取道泛型里面.
命名空间:using System.Collections.Generic;
public static List<News> GetTopNews(int NewsKindId,int IntTop) { List<News> AllNews = new List<News>(); string procname = "dbo.GetTopNews"; SqlParameter[] prams ={ new SqlParameter("@NewsKindId", SqlDbType.Int), new SqlParameter("@IntTop", SqlDbType.Int)}; prams[0].Value = NewsKindId; prams[1].Value = IntTop; SqlDataReader Dr = DataBase.RunProcGetReader(procname,prams); while (Dr.Read()) { AllNews.Add(new News(Dr)); } Dr.Close(); return AllNews; }
在News类中:
public News(SqlDataReader Dr) { this._newsid = Convert.ToInt32(Dr["NewsId"]); this._newstitle = Convert.ToString(Dr["NewsTitle"]); this._newsbody = Convert.ToString(Dr["NewsBody"]); this._newskindid = Convert.ToInt32(Dr["NewsKindId"]); this._userid = Convert.ToInt32(Dr["UserId"]); this._pubdate = Convert.ToDateTime(Dr["PubDate"]); }