2012-2-6
聊天室制作流程: 聊天室制作流程:
建立发送页面 send.aspx,显示页面 showmessage.aspx,主要是这两个,还可以加显示所以用 户名字 exit.aspx; 思路: 聊天室要用到数据库,所以在发送页面需要做的是把用户填好的东西加入到数据库储存起 来。 到显示页面和显示所以用户页面就顺理成章的把数据库用户填写的信息读取出来这就构 成了一个聊天室的主要骨架。
第一个发送页面 send.aspx 前台代码:
后台:send.aspx.cs
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
}
public DBConnection conn = new DBConnection(); protected void ButtonSpeak_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
connection.Open();
string createTime = System.DateTime.Now.ToString();//发言时间 string content = TextBoxContent.Text; string color = DropDownListColor.SelectedItem.Value; string emotion = DropDownListEmotion.SelectedItem.Value; string sql; //发言内容 //颜色 //表情
//SqlCommand cmd = new SqlCommand(sql, connection);
//SqlDataReader dr = conn.ExecuteReader(sql); sql = "Insert into message(username,createtime,content1,color,emotion) values('"+
Session["username"]+ "','" + createTime+ "','" +content + "','" + color + "','" + emotion + "')";
conn.Updata(sql);
TextBoxContent.Text=""; }
protected void ButtonExit_Click(object sender, EventArgs e) { Session["username"] = null; Response.Redirect("re.aspx"); } }
截图:
---------
----------------------------------------------------------------------------------------------------------------------------------------
说明: 这里因为要用到数据库
sql所以在开头加上命名空间:using System.Data.SqlClient;
还有链接数据库的
web.config 文件
Web。Config
这里填你 sql 数据库 (待会 i 给你)
这里填链接 你 sql 登录密 码吗和用户 value="Data
Source=V2WSJTEFQGV0NFU\SQLEXPRESS;database=chatroom;UID=yi;password=123456;"/>
新建
中加入
void Session_Start(object sender, EventArgs e) { // 在新会话启动时运行的代码
Application.Lock(); Session["username"] = ""; Application.UnLock();
}
--------------------------------------------------------------------------------------------------------------------------------------------------
显示页面 showmessage.sapx 前台:
<HTML XMLNS="W3.ORG xhtml? 1999>
后台 showmessage.aspx.cx
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class showmessage : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
//gridviewblind();
SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]); string sql = "select connection.Open(); * from message order by createtime";
SqlCommand cmd = new SqlCommand(sql, connection);
SqlDataReader da = cmd.ExecuteReader();
while (da.Rea
d()) {
Response.Write("【"+da["username"].ToString()+"】"); Response.Write(da["emotion"].ToString()); Response.Write("说道:");
Response.Write(da["createtime"].ToString());
//发言时间
Response.Write("
"); Response.Write(""); Response.Write(da["content1"].ToString()); Response.Write("
"); } //颜色 //发言内容
} } 这个就是把数据库中用户输入的数据读取出来的页面,每隔4秒刷新一次;
显示用户 exit.sapx
前台:
后台exit.aspx.cs:
using System; usin