分享
 
 
 

.net中即时消息发送的实现

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

数据库设计: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("<script language=JavaScript>window.open('info.aspx','','height=330,width=560,

status=no,location=no,toolbar=no,directories=no,menubar=no')</script>")

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("<script language=javascript>alert('发送成功!')</script>")

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"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<title></title>

<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">

<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">

<meta content="JavaScript" name="vs_defaultClientScript">

<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

</HEAD>

<body background="image/bg.gif" MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

<FONT face="宋体">

<asp:image id="Image3" style="Z-INDEX: 111; LEFT: 141px; POSITION: absolute; TOP: 312px" runat="server" Width="221px" Height="98px" ImageUrl="image/99re1-1.gif"></asp:image>

<asp:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 73px; POSITION: absolute; TOP: 123px" runat="server" BorderColor="Navy" BorderWidth="1px"></asp:textbox>

<asp:label id="Label1" style="Z-INDEX: 102; LEFT: 26px; POSITION: absolute; TOP: 1

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有