熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.net中即時消息發送的實現

2022-06-13   來源: .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=

Dim comm As SqlCommand = New SqlCommand(mysql conn)

commParametersAdd(New SqlParameter(@myid SqlDbTypeInt ))

commParameters(@myid)Value = Session(stu_id)

Dim dr As SqlDataReader

connOpen()

dr = commExecuteReader

If drRead Then

ResponseWrite( )

End If

drClose()

commCancel()

下面的代碼是用來發送即時消息的頁面其中裡面分了兩個部分一個是用來回復的一個是用來專門發送的兩個的頁面稍有區別仔細看一下就會明白的

下面是所有的代碼codebehind部分

  Public Sub Page_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad

If Not IsPostBack Then

Dim tostu_id As String = RequestQueryString(tostu_id)

If tostu_id = Then

//////////////////當回復留言時

Dim sql As String = select a*bnick from info apwd b where afromstu_id=bstu_id and atostu_id= & Session(stu_id) & and aterm=

Dim comm As SqlCommand = New SqlCommand(sql conn)

Dim dr As SqlDataReader

connOpen()

dr = commExecuteReader

While drRead

LabelText = drItem(nick)

LabelText = drItem(tim)

LabelText = drItem(content)

TextBoxText = drItem(nick)

TextBoxText = drItem(fromstu_id)

TextBoxEnabled = False

LabelVisible = False

End While

drClose()

commCancel()

//////////////////////更新留言使留言屬性為已閱讀過

Dim sql_ As String = update info set term= where tostu_id= & Session(stu_id) & and term= and tim= & LabelText &

comm = New SqlCommand(sql_ conn)

commExecuteNonQuery()

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

connOpen()

dr = commExecuteReader

While drRead

TextBoxText = em(nick)

End While

TextBoxEnabled = False

LabelText =

LabelText =

LabelVisible = False

LabelVisible = True

LabelVisible = False

LabelVisible = False

LabelVisible = False

drclose()

End If

End If

End Sub

/////////////////書寫提交消息事件

Public Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

Dim tostu_id As String = RequestQueryString(tostu_id)

If tostu_id = Then

/////////////////////////當回復留言時

connOpen()

Dim sql As String = insert into info(fromstu_idtostu_idcontenttermtim) values(@fromstu_id@tostu_id@content@term@tim)

Dim comm As SqlCommand = New SqlCommand(sql conn)

commParametersAdd(New SqlParameter(@fromstu_id SqlDbTypeInt ))

commParameters(@fromstu_id)Value = Session(stu_id)

commParametersAdd(New SqlParameter(@tostu_id SqlDbTypeInt ))

commParameters(@tostu_id)Value = TextBoxText

commParametersAdd(New SqlParameter(@content SqlDbTypeVarChar ))

commParameters(@content)Value = TextBoxText

commParametersAdd(New SqlParameter(@term SqlDbTypeInt ))

commParameters(@term)Value =

commParametersAdd(New SqlParameter(@tim SqlDbTypeChar ))

commParameters(@tim)Value = DateNow

commExecuteNonQuery()

TextBoxText =

Else

/////////////////////////當發送留言時

connOpen()

Dim sql As String = insert into info(fromstu_idtostu_idcontenttermtim) values(@fromstu_id@tostu_id@content@term@tim)

Dim comm As SqlCommand = New SqlCommand(sql conn)

commParametersAdd(New SqlParameter(@fromstu_id SqlDbTypeInt ))

commParameters(@fromstu_id)Value = Session(stu_id)

commParametersAdd(New SqlParameter(@tostu_id SqlDbTypeInt ))

commParameters(@tostu_id)Value = tostu_id

commParametersAdd(New SqlParameter(@content SqlDbTypeVarChar ))

commParameters(@content)Value = TextBoxText

commParametersAdd(New SqlParameter(@term SqlDbTypeInt ))

commParameters(@term)Value =

commParametersAdd(New SqlParameter(@tim SqlDbTypeChar ))

commParameters(@tim)Value = DateNow

commExecuteNonQuery()

TextBoxText =

End If

ResponseWrite( )

End Sub

////////////////////返回繼續發送

Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

ResponseRedirect(boamanaspx)

End Sub

End Class


From:http://tw.wingwit.com/Article/program/net/201311/11542.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.