当前位置:编程学习 > C#/ASP.NET >>

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

答案:用了我一下午的时间终于写完并整理好了利用.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("<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
           

上一个:个性化查询(具有分类模糊查询、换页等功能)
下一个:用vb.net做的校友录……(附所有源代码)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,