聊天室设计了两个表,一个表用于存放用户的登录信息,一个表用于存放用户的留言信息。
在 Models中定义的两个实体类Message和User
在DAL中定义两个类UserDAL和MessageDAL
UserDAL中
public static User GetUser(string userName)
{
string str = "select * from T_user where userName=@userName";
SqlParameter[] para = {new SqlParameter("@userName",SqlDbType.NVarChar) };
para[0].Value = userName;
SqlHelper helper = new SqlHelper();
DataTable dt = helper.ExecuteReturnDataTable(str,para,CommandType.Text);
if (dt.Rows.Count > 0)
{
//找到该用户信息后,保存在user对象中,并且返回这个对象
User user = new User();
user.UserId = (int)(dt.Rows[0]["userId"]);
user.UserName = (string)(dt.Rows[0]["userName"].ToString());
user.UserPsd = (string)(dt.Rows[0]["userPsd"].ToString());
return user;
}
else
{
//没有找到用户
return null;
}
MessageDAL中
public class MessageDAL
{
/// <summary>
/// 向信息表中添加信息
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static bool Addmessage(Message message)
{
string str = "insert into T_message(messageTime,userName,emotion,messageContent,color,face) values(@messageTime,@userName,@emotion,@messageContent,@color,@face)";
SqlParameter[] para = {
new SqlParameter("@messageTime",SqlDbType.DateTime),
new SqlParameter("@userName",SqlDbType.NVarChar),
new SqlParameter("@emotion",SqlDbType.NVarChar),
new SqlParameter("@messageContent",SqlDbType.NVarChar),
new SqlParameter("@color",SqlDbType.NVarChar),
new SqlParameter("@face",SqlDbType.NVarChar)
};
para[0].Value = message.MessageTime;
para[1].Value = message.UserName;
para[2].Value = message.Emotion;
para[3].Value = message.MessageContent;
para[4].Value = message.Color;
para[5].Value = message.Face;
SqlHelper helper = new SqlHelper();
int result = -1;
result = helper.ExecuteReturnInt(str, para, CommandType.Text);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 把数据行打包成Message数据实体
/// </summary>
/// <param name="dataRow">一条记录</param>
/// <returns>Message实体</returns>
public static Message GetMessageFromDataRow(DataRow dataRow)
{
Message message = new Message();
message.UserName = dataRow["userName"].ToString();
message.MessageContent = dataRow["messageContent"].ToString();
message.Emotion = dataRow["emotion"].ToString();
message.Color = dataRow["color"].ToString();
message.MessageTime = dataRow["messageTime"].ToString();
message.Face = dataRow["face"].ToString();
return message;
}
/// <summary>
/// 获得信息表中前10条用户的数据
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static List<Message> Selectmessage()
{
List<Message> messagelist = new List<Models.Message>();
string str = "select top(10) * from T_message order by messageTime desc";
SqlHelper helper = new SqlHelper();
DataTable dt = helper.ExecuteReturnDataTable(str, null, CommandType.Text);
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
messagelist.Add(GetMessageFromDataRow(row));
}
return messagelist;
}
return null;
}
}
在BLL中定义MessageBLL和UserBLL
public class MessageBLL
{
/// <summary>
/// 获得信息表中前10条用户的数据
/// </summary>
/// <returns></returns>
public static List<Message> Selectmessage()
{
return MessageDAL.Selectmessage();
}
/// <summary>
/// 发表留言
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static bool Addmessage(Message message)
{
return MessageDAL.Addmessage(message);
}
}
UserBLL中
public class UserBLL
{
/// <summary>
/// 登陆验证
/// </summary>
public static bool login(string userName,string userPsd,out User validUser)
{
User user = UserDAL.GetUser(userName);
//没有该用户信息
if (user == null)
{
validUser = null;
return false;
}
//如果登陆的用户名与数据库中的用户名相同时,该用户为合法用户
if (user.UserName == userName)
{
validUser = user;
return true;
}
else
{
validUser = null;
return false;
}
}
}
在用户界面层
登录界面
protected void bt_Login_Click(object sender, EventArgs e)
{
User user;
if (this.txt_username.Text.Trim().Length == 0)
{
Response.Write("<script>alert('请输入用户名')</script>");
}
if (this.txt_userpsd.Text.Trim().Length == 0)
{
Response.Write("<script>alert('请输入密码')</script>");
}
if (UserBLL.login(this.txt_username.Text, this.txt_userpsd.Text, out user))
{
Session["user"] = user;
Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('您输入的用户名或者密码错误')</script>");
}
}
聊天室页面
aspx
<div>
<div>
<p align="center" style="height: 41px; font-style: normal; font-size: x-large; color: Blue;">
*欢迎<asp:Label ID="L_name" runat="server" Text=""></asp:Label>来到MyChatRoom*<br />
<asp:Button ID="bt_return" runat="server" Text="返回登录" OnClick="bt_return_Click" /></p>
</div>
<table style="width: 100%;">
<tr>
<td class="style1">
<iframe src="Iframe.aspx" scrolling="yes" style="width: 654px"></iframe>
</td>
</tr>
<tr>
<td class="style1">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/image/1.jpg"
οnclick="ImageButton1_Click" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/image/2.jpg"
οnclick="ImageButton2_Click" />
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/image/3.jpg"
οnclick="ImageButton3_Click"/>
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/image/4.jpg"
οnclick="ImageButton4_Click"/>
<asp:ImageButton ID="ImageButton5" runat="server" ImageUrl="~/image/5.jpg"
οnclick="ImageButton5_Click"/>
</td>
</tr>
<tr>
<td class="style1">
<asp:TextBox ID="txt_content" runat="server" TextMode="MultiLine" Height="48px" Width="447px"></asp:TextBox>
</td>
</tr>
</table>
<table style="width: 40%;">
<tr>
<td align="right">
<asp:DropDownList ID="ddl_color" runat="server">
<asp:ListItem Value="black" Selected="True">黑色</asp:ListItem>
<asp:ListItem Value="red">红色</asp:ListItem>
<asp:ListItem Value="blue">蓝色</asp:ListItem>
<asp:ListItem Value="purple">紫色</asp:ListItem>
</asp:DropDownList>
</td>
<td align="left">
<asp:DropDownList ID="ddl_emotion" runat="server">
<asp:ListItem Value="微笑的" Selected="True">微笑的</asp:ListItem>
<asp:ListItem Value="高兴的">高兴的</asp:ListItem>
<asp:ListItem Value="伤心的">伤心的</asp:ListItem>
<asp:ListItem Value="沮丧的">沮丧的</asp:ListItem>
</asp:DropDownList>
</td>
<td align="left">
<asp:Button ID="bt_talk" runat="server" Text="发言" OnClick="bt_talk_Click" />
</td>
<td align="left">
<asp:Button ID="bt_leave" runat="server" Text="离开" OnClick="bt_leave_Click" />
</td>
</tr>
</table>
</div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["user"] != null)
{
L_name.Text = ((User)(Session["user"])).UserName;
}
else
{
Response.Write("<script>alert('请先登录');location.href('Login.aspx')</script>");
}
}
}
/// <summary>
/// 发言
/// </summary>
protected void bt_talk_Click(object sender, EventArgs e)
{
if (Session["user"] != null)
{
string messageinfo = txt_content.Text.Trim();
//使用正则表达式的Replace方法
string result1 = Regex.Replace(messageinfo, @"{!", "<img src='image/");
string result2 = Regex.Replace(result1, @"!}", ".jpg'/>");
Message mess = new Message();
DateTime date = DateTime.Now;
mess.MessageTime = date.ToString();
mess.UserName = ((User)(Session["user"])).UserName;
mess.Emotion = ddl_emotion.SelectedValue;
mess.MessageContent = result2;
mess.Color = ddl_color.SelectedValue;
mess.Face = "";
bool result=MessageBLL.Addmessage(mess);
if (result==true)
{
Response.Write("<script>alert('发言成功')</script>");
}
else
{
Response.Write("<script>alert('发言失败')</script>");
}
}
}
/// <summary>
/// 离开
/// </summary>
protected void bt_leave_Click(object sender, EventArgs e)
{
//清除对象
Session.Contents.Remove("user");
Response.Redirect("Login.aspx");
}
/// <summary>
/// 返回登录
/// </summary>
protected void bt_return_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
/// <summary>
/// 在文本框中插入表情
/// </summary>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
txt_content.Text += "{!1!}"; //1是图片的名称,下同
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
txt_content.Text += "{!2!}";
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
txt_content.Text += "{!3!}";
}
protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
{
txt_content.Text += "{!4!}";
}
protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{
txt_content.Text += "{!5!}";
}
Iframe.aspx
<form id="form1" runat="server">
<div>
<asp:DataList ID="dl_iframe" runat="server">
<ItemTemplate>
<div style=' color:<%#Eval("color")%>'>
<%#Eval("messageTime") %>
<%#"("+Eval("userName")+")" %>
<%#Eval("emotion")+"说:" %>
<%#Eval("messageContent") %>
</div>
</ItemTemplate>
</asp:DataList>
</div>
</form>
Iframe.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//messagelist就相当于一个数据表
List<Message> messagelist = MessageBLL.Selectmessage();
dl_iframe.DataSource = messagelist;
dl_iframe.DataBind();
}
}
在三层架构中,在DAL层中的数据要以表的形式在页面中显示,可以用List来实现,如果List<Message> messagelist = new List<Models.Message>();
Message message = new Message();
定义一个messagelist对象,将一个个message实体放在里面,然后在aspx.cs 中可以直接将messagelist赋给数据源,
如List<Message> messagelist = MessageBLL.Selectmessage();
dl_iframe.DataSource = messagelist;
dl_iframe.DataBind();
1、 Models---为实体类库
实体类库是作为三成架构的辅助类库存在,数据在传递时都是传递一个封装好的实体;
数据库中一个表对应一个实体
2、 DAL----DATA ACCESS LAYER:数据访问层
数据库中一个表对应一个类文件,其中定义对这个表的增删改查等具体的数据库操作;
SQLhelper是最基本的数据库访问类,也放在该类库中
3、 BLL----Business Logic Layer:业务逻辑层
业务逻辑层中的类对具体的业务进行定义,需要调用DAL层去执行命令
4、Web(UI)---User Interface Layer:用户界面层
用户界面层是直接和用户打交道的,UI层要调用BLL层的类来实现具体的操作