用了我一下午的时间终于写完并整理好了利用.net来发送即时消息的材料(当然了,还有上午的数据库设计:)
数据库设计:info表:id fromstu_id tostu_id content term
其中id是主键,fromstu_id是发送信息的用户的学号(这是和我做的学友录连在一起的),tostu_id是接受信息的用户的学号,content是消息的内容,term是判断是否为新消息。
下面的代码家在校友录中的if not ispostback中
/////////////////////判断是否有新留言,将自动弹出页面
这里还要将页面的刷新时间设置一下,以便可以循环的读取信息。
dim mysql as string = "select * from info where tostu_id=@myid and term=1"
dim comm as sqlcommand = new sqlcommand(mysql, conn)
comm.parameters.add(new sqlparameter("@myid", sqldbtype.int, 4))
comm.parameters("@myid").value = session("stu_id")
dim dr as sqldatareader
conn.open()
dr = comm.executereader
if dr.read then
response.write("")
end if
dr.close()
comm.cancel()
下面的代码是用来发送即时消息的页面,其中里面分了两个部分,一个是用来回复的,一个是用来专门发送的,两个的页面稍有区别,仔细看一下就会明白的:)
下面是所有的代码:codebehind部分
public sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
if not ispostback then
dim tostu_id as string = request.querystring("tostu_id")
if tostu_id = "" then
//////////////////当回复留言时
dim sql as string = "select a.*,b.nick from info a,pwd
b where a.fromstu_id=b.stu_id and a.tostu_id=" & session("stu_id")
& " and a.term=1"
dim comm as sqlcommand = new sqlcommand(sql, conn)
dim dr as sqldatareader
conn.open()
dr = comm.executereader
while dr.read
label3.text = dr.item("nick")
label4.text = dr.item("tim")
label5.text = dr.item("content")
textbox1.text = dr.item("nick")
textbox3.text = dr.item("fromstu_id")
textbox1.enabled = false
label8.visible = false
end while
dr.close()
comm.cancel()
//////////////////////更新留言使留言属性为已阅读过
dim sql_1 as string = "update info set term=0 where
tostu_id=" & session("stu_id") & " and term=1 and tim=" &
label4.text & ""
comm = new sqlcommand(sql_1, conn)
comm.executenonquery()
else
////////////////////当发送留言时
dim mysql as string = "select nick from pwd where stu_id=" & tostu_id & ""
dim comm as sqlcommand = new sqlcommand(mysql, conn)
dim dr as sqldatareader
conn.open()
dr = comm.executereader
while dr.read
textbox1.text = dr.item("nick")
end while
textbox1.enabled = false
label3.text = ""
label4.text = ""
label5.visible = false
label8.visible = true
label6.visible = false
label7.visible = false
label9.visible = false
dr.close()
end if
end if
end sub
/////////////////书写提交消息事件
public sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim tostu_id as string = request.querystring("tostu_id")
if tostu_id = "" then
/////////////////////////当回复留言时
conn.open()
dim sql as string = "insert into
info(fromstu_id,tostu_id,content,term,tim)
values(@fromstu_id,@tostu_id,@content,@term,@tim)"
dim comm as sqlcommand = new sqlcommand(sql, conn)
comm.parameters.add(new sqlparameter("@fromstu_id", sqldbtype.int, 4))
comm.parameters("@fromstu_id").value = session("stu_id")
comm.parameters.add(new sqlparameter("@tostu_id", sqldbtype.int, 4))
comm.parameters("@tostu_id").value = textbox3.text
comm.parameters.add(new sqlparameter("@content", sqldbtype.varchar, 200))
comm.parameters("@content").value = textbox2.text
comm.parameters.add(new sqlparameter("@term", sqldbtype.int, 4))
comm.parameters("@term").value = "1"
comm.parameters.add(new sqlparameter("@tim", sqldbtype.char, 20))
comm.parameters("@tim").value = date.now
comm.executenonquery()
textbox2.text = ""
else
/////////////////////////当发送留言时
conn.open()
dim sql as string = "insert into
info(fromstu_id,tostu_id,content,term,tim)
values(@fromstu_id,@tostu_id,@content,@term,@tim)"
dim comm as sqlcommand = new sqlcommand(sql, conn)
comm.parameters.add(new sqlparameter("@fromstu_id", sqldbtype.int, 4))
comm.parameters("@fromstu_id").value = session("stu_id")
comm.parameters.add(new sqlparameter("@tostu_id", sqldbtype.int, 4))
comm.parameters("@tostu_id").value = tostu_id
comm.parameters.add(new sqlparameter("@content", sqldbtype.varchar, 200))
comm.parameters("@content").value = textbox2.text
comm.parameters.add(new sqlparameter("@term", sqldbtype.int, 4))
comm.parameters("@term").value = "1"
comm.parameters.add(new sqlparameter("@tim", sqldbtype.char, 20))
comm.parameters("@tim").value = date.now
comm.executenonquery()
textbox2.text = ""
end if
response.write("")
end sub
////////////////////返回继续发送
private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
response.redirect("boaman.aspx")
end sub
end class
页面部分:
<%@ page language="vb" autoeventwireup="false" codebehind="info.aspx.vb" inherits="_99re1.info"%>