SqlDataReader使用实例
public string GetTopNews() { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["demo"].ConnectionString); string sqlText = "SELECT TOP 50 * from dbo.normal_content WHERE classid='90'"; SqlCommand cmd = new SqlCommand(sqlText, conn); List<string> newList = new List<string>(); try { conn.Open(); SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); while (reader.Read()) { string newsli = string.Format("<li>标题:{0}, 时间:{1}</li>", reader["Title"], Convert.ToDateTime(reader["AddDate"]).ToString("yyyy-MM-dd")); newList.Add(newsli); } if (reader.IsClosed == false) reader.Close(); } catch (Exception ex) { } finally { if (conn.State != System.Data.ConnectionState.Closed) conn.Close(); } return string.Join("", newList.ToArray()); }
此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。

浙公网安备 33010602011771号