标签:des style blog color io os ar strong for
关于列表的动态输出
数据库的实时显示,可以更加合理管理自己的网站。下面讲一下数据更新的实时显示。
private string connStr = ConfigurationManager.ConnectionStrings["ruanmou"].ToString(); //2:实例化一个SqlConnection对象,用于和数据库建立起连接 SqlConnection con; SqlCommand cmd; SqlDataReader read; private SqlConnection ConDb() { con = new SqlConnection(connStr); con.Open(); return con; } public string SqlWhere() { StringBuilder sb = new StringBuilder(); sb.Append(string.Format("select top {0} * from RNews where 1=1 order by NewsId desc", 4)); return sb.ToString(); } public string Bind() { StringBuilder sb = new StringBuilder(); //List<RNews> list = RNewsDAL.m_rnews.GetList(SqlWhere()); //foreach (var a in list) //{ // sb.Append(a.Title); //} //return sb.ToString(); using (ConDb()) { string sql = SqlWhere(); cmd = new SqlCommand(SqlWhere(), con); using (read = cmd.ExecuteReader()) { if (read.Read()) { foreach (var news in read) { sb.Append(string.Format(@" <li><a target=""_blank"" href=""NewsPage.aspx?NewsId={0}"">【新闻】{1}</a></li> ", read["NewsId"], read["Title"])); } } } return sb.ToString(); }
这样子可以随时的看到自己发表的东西。但是还是存在一点问题,就是最后一条记录总是读写不出来,但是在数据库中,语句并没有问题,希望大神指教!
标签:des style blog color io os ar strong for
原文地址:http://www.cnblogs.com/xiaow/p/3990300.html