用了我一下午的時間終於寫完並整理好了利用.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
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").Valu
e = "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" runat="server" Width="221px" Height="98px" ImageUrl="image/99re1-1.gif"></ASP:image>
<asp:textbox id="TextBox1" runat="server" BorderColor="Navy" BorderWidth="1px"></ASP:textbox>
<asp:label id="Label1" runat="server" Width="42px" Height="18px" Font-Size="X-Small" ForeColor="Navy" Font-Bold="True">發往:</ASP:label>
<asp:label id="Label2" runat="server" Font-Size="X-Small" ForeColor="Navy" Font-Bold="True">內容:</ASP:label>
<asp:textbox id="TextBox2" runat="server" TextMode="MultiLine" Width="449px" Height="74px" BorderColor="Navy" BorderWidth="1px" MaxLength="200"></ASP:textbox>
<asp:button id="Button1" runat="server" Width="50px" Height="20px" Text="發送" BorderColor="Navy" BorderWidth="1px" BackColor="#FFE0C0"></ASP:button>
<a
sp:button id="Button2" runat="server" Width="87px" Height="20px" Text="繼續發送…" BorderColor="Navy" BorderWidth="1px" BackColor="#FFE0C0"></ASP:button>
<asp:label id="Label3" runat="server" Width="135px" Height="6px" Font-Size="Small">Label</ASP:label>
<asp:label id="Label4" runat="server" Width="219px" Height="13px" Font-Size="Small">Label</ASP:label>
<asp:label id="Label5" runat="server" Width="447px" Height="71px" Font-Size="X-Small" BorderColor="SlateGray" BorderWidth="1px">Label</ASP:label>
<asp:label id="Label6" runat="server" Font-Size="X-Small" ForeColor="Red" Font-Bold="True">來自:</ASP:label>
<asp:TextBox id="TextBox3" runat="server" Visible="False"></ASP:TextBox>
<asp:Label id="Label8" runat="server" Height="33px" Width="327px" Font-Bold="True" ForeColor="Navy" Font-Size="Large" Font-Names="方正姚體" Font-Underline="True">直接寫入內容點擊發送即可!</ASP:Label>
<asp:Label id="Label7" runat="server" Height="15px" Width="71px" Font-Bold="True" ForeColor="Red" Font-Size="X-Small">發信日期:</ASP:Label>
<asp:Label id="Label9" runat="server" Font-Bold="True" ForeColor="Red" Font-Size="X-Small">內容:</ASP:Label>
</FONT>
</form>
</body>
</Html>
以上代碼在bata2環境下調試成功.