📄 main.aspx.cs
字号:
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 main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Register Ajax.NET methods from this class
AjaxPro.Utility.RegisterTypeForAjax(typeof(main));
if (!IsPostBack)
{
// Show the login user name
//Page.User property--get the info of the user who sends out the page request
lblUsername.Text = User.Identity.Name;
}
}
/// <summary>
/// Get the online user list
/// </summary>
/// <returns>online user DataSet</returns>
[AjaxPro.AjaxMethod]
public DataSet GetOnlineUserList()
{
//to hold the returned DataSet
DataSet ds = new DataSet();
//the data connection
string strConnection = ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
// SQL command
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT username, nickname FROM users WHERE status = 1 AND username <> '"
+ User.Identity.Name + "'";
try
{
// fill in the friend messages into the DataSet
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch (SqlException)
{
}
finally
{
conn.Close();
}
return ds;
}
/// <summary>
/// Obtain the changing times inside table users
/// </summary>
/// <returns>The changing times inside table users</returns>
[AjaxPro.AjaxMethod]
public int StatusChanged()
{
// to hold the returned value
int iRet = 0;
// Obtain the database connection string from the file web.config
string strConnection = ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
// SQL command
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT UsersChanged FROM global_info";
try
{
//open the data connection
conn.Open();
// get the value of field UsersChanged
//execute the query and return the value in the first line and the first column
//from inside the result DataSet--ignore all the others
iRet = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (SqlException)
{
}
finally
{
//close the database connection
conn.Close();
}
return iRet;
}
/// <summary>
/// Check the user status
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public int CheckStatus()
{
//to hold the returned value
int iRet = 0;
//Obtain the database connection string from the file web.config
string strConnection = ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
// SQL command
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT status FROM users WHERE username = '" +
User.Identity.Name + "'";
try
{
// open the data connection
conn.Open();
// return the current user related value of field status
iRet = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (SqlException)
{
}
finally
{
// close the database connection
conn.Close();
}
return iRet;
}
/// <summary>
/// Get the most recent messages
/// </summary>
/// <returns>The most recent messages related DataSet</returns>
[AjaxPro.AjaxMethod]
public DataSet mainGetNewMessage()
{
//to hold the returned DataSet
DataSet ds = new DataSet();
//Obtain the database connection string from the file web.config and set up the 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);
// the data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
//fill the messages into the DataSet
da.Fill(ds);
}
catch (SqlException)
{
}
finally
{
//close the database connection
conn.Close();
}
return ds;
}
/// <summary>
///Set the user status
/// </summary>
/// <param name="strUsername">user name</param>
/// <param name="iStatus">status value</param>
/// <returns>whether the query is successful</returns>
[AjaxPro.AjaxMethod]
public int SetUserStatus(string strUsername, int iStatus)
{
//to hold the returned value
int iRet = 0;
//Obtain the database connection string from the file web.config and set up the connection
string strConnection = ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
// SQL command
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format(
"UPDATE users SET status = {0} WHERE username='{1}'",
iStatus, strUsername
);
try
{
//open the data connection
conn.Open();
// execute the SQL command-- set the user's status
iRet = cmd.ExecuteNonQuery();
}
catch (SqlException)
{
}
finally
{
//close the database connection
conn.Close();
}
return iRet;
}
protected void lblExit_Click(object sender, EventArgs e)
{
//Obtain the database connection string from the file web.config and set up the connection
string strConnection = ConfigurationManager.ConnectionStrings["msn_Data_ConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
// SQL command
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format("UserLogout '{0}'", User.Identity.Name);
try
{
//open the data connection
conn.Open();
// execute the SQL command--logout
cmd.ExecuteNonQuery();
}
catch (SqlException)
{
}
finally
{
//close the database connection
conn.Close();
}
//logout
FormsAuthentication.SignOut();
// redirect to the login page
Response.Redirect("Login.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -