chatroom.aspx.cs

来自「this is the file to chat in web」· CS 代码 · 共 134 行

CS
134
字号
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
[AjaxPro.AjaxNamespace("AjaxProChat")]
public partial class chatroom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //note the initialization code for the framework AjaxPro.NET
        // Register Ajax.NET methods from this class
        AjaxPro.Utility.RegisterTypeForAjax(typeof(chatroom));

    }
    /// <summary>
    /// Get the recent messages
    /// </summary>
    /// <param name="strUsername">Username</param>
    /// <returns>the DataSet of the recent messages</returns>
    [AjaxPro.AjaxMethod]
    public  DataSet GetRecentMsg(string strUsername)
    {
        //the returned DataSet
        DataSet ds = new DataSet();

        //the data connection
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString);

        // SQL command
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = string.Format(
            "GetRecentMsg '{0}','{1}', {2}",
            User.Identity.Name, strUsername, 8);

        //data adapter definition
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        try
        {
            //fill in the messages
            da.Fill(ds);
        }
        catch (SqlException)
        {
        }
        finally
        {
            // close the connection
            conn.Close();
        }

        return ds;
    }

    /// <summary>
    /// Send messages
    /// </summary>
    /// <param name="strUsername">Username</param>
    /// <param name="strContent">Content</param>
    [AjaxPro.AjaxMethod]
    public  void SendMessage(string strUsername, string strContent)
    {
        // the data connection
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString);

        // SQL command
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = string.Format("SendMessage '{0}','{1}', '{2}'",
            User.Identity.Name, strUsername, strContent);

        try
        {
            // open the data connection
            conn.Open();

            // execute the SQL command
            cmd.ExecuteNonQuery();
        }
        catch (SqlException)
        {
        }
        finally
        {
            //close the connection
            conn.Close();
        }
    }

    /// <summary>
    ///obtain new messages
    /// </summary>
    /// <returns>the DataSet of the new messages</returns>
    [AjaxPro.AjaxMethod]
    public DataSet chatroomGetNewMessage()
    {
        // returned dataset
        DataSet ds = new DataSet();

        //data connection
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString);

        // SQL command
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = string.Format("GetNewMessage '{0}'", User.Identity.Name);

        // data adapter
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        try
        {
            // fill messages data into DataSet
            da.Fill(ds);
        }
        catch (SqlException)
        {
        }
        finally
        {
            //close the connection
            conn.Close();
        }

        return ds;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?