数据库连接代码
dbname="chatroom.mdb"
set conn=server.createobject("adodb.connection")
connstr="dbq="+server.mappath(dbname)+";driver={microsoft access driver (*.mdb)};"
conn.open connstr
%>
html页面代码
postmsg
&toname=" method="post" name="form1">
">
对
大家
'显示在线人数
set rsout=server.createobject("adodb.recordset")
sqlout="select * from online where id order by id desc" '从数据苦中查许所有记录,按照id降序排列
rsout.open sqlout,conn,1,1 '执行查询,将结果保存在rsout中
for i=1 to 50 '在下拉框中最多显示50个用户名
%>
rsout.movenext '指向下一条记录
if rsout.eof then exit for '到达最后一条记录则跳出for循环
next
%>
说:
">
处理页面可保存在同一页面
ac=request.querystring("act") '读取act 的值
if ac="addmsg" then 'act的值为addmsg
if request.form("hidden")"" then '私聊
session("chat")="" '清空session("chat")中的内容
'把要显示的私聊信息保存在application("chat")中
application("chat")=""&request.form("username")&"悄悄地对"&request.form("rname")&"说:"&request.form("msg")&""&time()&""
application("chatto")=request.form("rname") '聊天对象
application("owner")=request.form("username") '发言者
else '不是私聊
set rs=server.createobject("adodb.recordset") '创建recordset对象
sql="select * from chat" '查询数据库
rs.open sql,conn,1,3 '执行查询
rs.addnew '添加一条新记录
rs("fromname")=request.form("username") '发言者
rs("toname")=request.form("rname") '聊天对象
rs("chat")=request.form("msg") '显示的私聊信息
rs("posttime")=time() '提交时间
rs.update
rs.close
set rs=nothing
'增加一条聊天记录,将application("gchatnum")的值加1
application("gchatnum")=application("gchatnum")+1
end if
end if
%>源码下载地址