数据库模型类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// Users 的摘要说明
/// </summary>
public class Users
{
SqlConnection conn = null;
SqlCommand cmd = null;
public Users()
{
conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;");
cmd = conn.CreateCommand();
}
public string UserName { get; set; }
public string PassWord { get; set; }
public string NickName { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
public string Nation { get; set; }
public string SexStr {
get {
return Sex ? "男" : "女";
}
}
public string NationName {
get {
string end = "<无>";
cmd.CommandText = "select *from Nation where NationCode=@a";
cmd.Parameters.Clear();
cmd.Parameters.Add("@a", Nation);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
end = dr["NationName"].ToString();
}
conn.Close();
return end;
}
}
}
数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// UsersData 的摘要说明
/// </summary>
public class UsersData
{
SqlConnection conn = null;
SqlCommand cmd = null;
public UsersData()
{
conn = new SqlConnection("server=.;database=Data0425;usser=sa;pwd=123;");
cmd = conn.CreateCommand();
}
public bool SelectUsers(string uname, string upass)
{
bool has = false;
cmd.CommandText = "select * from users where username=@a and password=@b";
cmd.Parameters.Clear();
cmd.Parameters.Add("@a", uname);
cmd.Parameters.Add("@b", upass);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
has = true;
}
conn.Close();
return has;
}
/// <summary>
/// 查询全部的USERS信息
/// </summary>
/// <returns></returns>
public List<Users> SelectAll()
{
List<Users> list = new List<UsersData>();
cmd.CommandText="select * from Users";
conn.Open();
SqlDataAdapter dr=cmd.ExecuteReader();
}
if (dr.hasrows)
{
Users u=new Users();
u.username =dr[0].tostring();
u.password=dr[1].tostring();
u.nickname=dr[2].tostring();
u.sex=Convert.ToBoolean(dr[3]);
u.birthday=Convert.ToDateTime(dr[4]);
u.nation=dr[5].Tostring();
List.Add(u);
}
}
conn.close();
return list;
主页操作
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
table {
background-color: navy;
100%;
}
#tr_head {
color: white;
font-size: 16px;
text-align: center;
font-family: 微软雅黑;
}
.tr_item {
background-color: white;
text-align: center;
}
td {
padding: 10px;
}
.tr_alt {
background-color:#e0e0e0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1>这里是主页面</h1>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr id="tr_head">
<td style=" 15%;">用户名</td>
<td style=" 15%;">密码</td>
<td style=" 15%;">昵称</td>
<td style=" 15%;">性别</td>
<td style=" 25%;">生日</td>
<td style=" 15%;">民族</td>
</tr>
</HeaderTemplate>
<AlternatingItemTemplate>
<tr class="tr_item tr_alt" style="<%# ShowRed() %><%# ShowBold() %>">
<td><%#Eval("UserName") %></td>
<td><%#Eval("PassWord") %></td>
<td><%#Eval("NickName") %></td>
<td>
<img src="<%# SexImg() %>" /></td>
<td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td>
<td><%#Eval("NationName") %></td>
</tr>
</AlternatingItemTemplate>
<ItemTemplate>
<tr class="tr_item" style="<%# ShowRed() %><%# ShowBold() %>">
<td><%#Eval("UserName") %></td>
<td><%#Eval("PassWord") %></td>
<td><%#Eval("NickName") %></td>
<td>
<img src="<%# SexImg() %>" /></td>
<td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td>
<td><%#Eval("NationName") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>