using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// DataConnection 的摘要说明 /// </summary> public class DataConnection { private static string connstr = "server=.; database=mydb; user=sa; pwd=ray; "; public static SqlConnection Conn { get { return new SqlConnection (connstr);} } }
//UsersData using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// UsersData 的摘要说明 /// </summary> public class UsersData { private SqlConnection _conn; private SqlCommand _cmd; private SqlDataReader _dr; public UsersData() { _conn = DataConnection.Conn; _cmd = _conn.CreateCommand(); } /// <summary> /// 查询Users表全部数据 /// </summary> /// <returns></returns> public List<Users> SelectAll() { List<Users> list = new List<Users>(); _cmd.CommandText = "select *from Users"; _conn.Open(); _dr = _cmd.ExecuteReader(); if (_dr.HasRows) { while (_dr.Read()) { Users data = new Users(); data.Username = _dr["username"].ToString(); data.Password = _dr["password"].ToString(); data.Gender = Convert.ToBoolean(_dr["gender"]); data.Nickname = _dr["nickname"].ToString(); data.Nation = _dr["nation"].ToString(); data.Area = _dr["area"].ToString(); data.Birthday = Convert.ToDateTime(_dr["birthday"]); list.Add(data); } } _conn.Close(); return list; } } //NationData using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// NationData 的摘要说明 /// </summary> public class NationData { private SqlConnection _conn; private SqlCommand _cmd; private SqlDataReader _dr; public NationData() { _conn = DataConnection.Conn; _cmd = _conn.CreateCommand(); } /// <summary> /// 根据编号查询名族名称 /// </summary> /// <param name="ncode"></param> /// <returns></returns> public string SelectName(string ncode) { string name = ""; _cmd.CommandText = "select Name from Nation where Code=@code"; _cmd.Parameters.Clear(); _cmd.Parameters.Add("@code",ncode); _conn.Open(); _dr = _cmd.ExecuteReader(); if (_dr.HasRows) { _dr.Read(); name = _dr[0].ToString(); } _conn.Close(); return name; } }
//Users using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Users 的摘要说明 /// </summary> public class Users { public Users() { // // TODO: 在此处添加构造函数逻辑 // } private string _username; public string Username { get { return _username; } set { _username = value; } } private string _password; public string Password { get { return _password; } set { _password = value; } } private string _nickname; public string Nickname { get { return _nickname; } set { _nickname = value; } } private bool _gender; public bool Gender { get { return _gender; } set { _gender = value; } } private DateTime _birthday; public DateTime Birthday { get { return _birthday; } set { _birthday = value; } } private string _nation; public string Nation { get { return _nation; } set { _nation = value; } } private string _area; public string Area { get { return _area; } set { _area = value; } } //属性扩展 public string GenderName { get { return Gender ? "男" : "女"; } } public string BirthdayStr { get { return Birthday.ToString("yyyy年MM月dd日"); } } public string NationName { get { return new NationData().SelectName(Nation); } } public int Age { get { return Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(Birthday.ToString("yyyy")); } } //红色警告 public string Red { get { string end = ""; if (GenderName == "女") { end = "color:Red; font-weight:bold; font-style:initial; font-family:华文行楷;"; } return end; } } } //Nation using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Nation 的摘要说明 /// </summary> public class Nation { public Nation() { // // TODO: 在此处添加构造函数逻辑 // } private string _nationcode; public string Nationcode { get { return _nationcode; } set { _nationcode = value; } } private string _nationname; public string Nationname { get { return _nationname; } set { _nationname = value; } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style> * { padding:0px; margin:0px; } #table { position:absolute; left:10%; width:80%; background-color:purple; text-align:center; font-family:微软雅黑; } #tr_head { color:white; } .tr_body1 { background-color:white; } .tr_body2 { background-color:purple; } td { padding:5px; } </style> </head> <body> <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table id="table"> <tr id="tr_head"> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>年龄</td> <td>地区</td> <td>民族</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tr_body1" style="<%# Eval("Red")%>"> <td><%#Eval("Username") %></td> <td><%#Eval("Password") %></td> <td><%#Eval("Nickname") %></td> <td><%#Eval("GenderName") %></td> <td><%#Eval("BirthdayStr") %></td> <td><%#Eval("Age")+"岁" %></td> <td><%#Eval("Area") %></td> <td><%#Eval("NationName") %></td> </tr> </ItemTemplate> <%--<AlternatingItemTemplate> <tr class="tr_body2" style="<%# Eval("Red")%>"> <td><%#Eval("Username") %></td> <td><%#Eval("Password") %></td> <td><%#Eval("Nickname") %></td> <td><%#Eval("GenderName") %></td> <td><%#Eval("BirthdayStr") %></td> <td><%#Eval("Age") %></td> <td><%#Eval("Area") %></td> <td><%#Eval("NationName") %></td> </tr> </AlternatingItemTemplate>--%> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<Users> list = new UsersData().SelectAll(); Repeater1.DataSource = list; Repeater1.DataBind(); } } }
运行页面: