用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - asp代码库

ajax+asp.net+mssql无刷新聊天室

2014-12-19 作者: asp源码之家举报

[asp]代码库

if(e.ctrlKey&&e.keyCode==13){
 
          e.preventDefault();
 
          e.stopPropagation();
 
          setTimeout("Showbo.Chat.send()",50);//在ff中马上使用alert提示时尽然弹出下载工具,奇怪.
 
          return false;}
 
       },false);
 
 
 
 
 
using System;
 
using System.Web;
 
using System.Data;
 
using System.Configuration;
 
using System.Data.SqlClient;
 
public class Ajax
 
{
 
 
 
    private static bool IsNull(string v)
 
    {
 
        if (v == null || v.Trim() == ""return true;
 
        else return false;
 
    }
 
 
 
    private static string Js(string v)
 
    {
 
        return v.Replace("'""\\'");
 
    }
 
 
 
    public static string Login()
 
    {
 
        HttpRequest Request = HttpContext.Current.Request;
 
        string rStr = "";
 
 
 
        string UserName = Request.Form["nn"];
 
        if (IsNull(UserName))
 
        {
 
            rStr = "success:false,err:'昵称不能为空!'";
 
        }
 
        else if (UserName.Length > 20)
 
        {
 
            rStr = "success:false,err:'昵称不能超过20个字符!'";
 
        }
 
        else
 
        {
 
            string UserId = "", Key = "";
 
            SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
 
            cn.Open();
 
            try
 
            {
 
                SqlCommand cm = new SqlCommand("ajaxLogin", cn);
 
                cm.CommandType = CommandType.StoredProcedure;
 
                cm.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50));
 
                cm.Parameters["@UserName"].Value = UserName;
 
                //==========输出参数
 
                cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
 
                cm.Parameters["@UserId"].Direction = ParameterDirection.Output;
 
                cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
 
                cm.Parameters["@UserKey"].Direction = ParameterDirection.Output;
 
                 
 
                cm.ExecuteNonQuery();
 
                UserId = cm.Parameters["@UserId"].Value.ToString().Trim();
 
                Key = cm.Parameters["@UserKey"].Value.ToString().Trim();
 
                if (UserId == "-1") rStr = @"success:false,err:'发生错误,请稍后再试!'";
 
                else if (UserId == "0") rStr = @"success:false,err:'已经存在此用户昵称,请修改您的昵称!'";
 
                else rStr += "success:true,UserId:'" + UserId + "',Key:'" + Key + "'";
 
                cm.Dispose();
 
            }
 
            catch (Exception e)
 
            {
 
                rStr = @"success:false,err:'原因\\n" + Js(e.Message) + "'";
 
            }
 
            cn.Close();
 
        }
 
 
 
        return rStr;
 
    }
 
 
 
    public static string Logout()
 
    {
 
        HttpRequest Request = HttpContext.Current.Request;
 
        string rStr = "", UserId = Request.Form["uid"], Key = Request.Form["key"];
 
        if (IsNull(UserId) || IsNull(Key)) return "success:false,err:'用户信息丢失!'";
 
 
 
        SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
 
        cn.Open();
 
        try
 
        {
 
            SqlCommand cm = new SqlCommand("ajaxLogout", cn);
 
            cm.CommandType = CommandType.StoredProcedure;
 
            cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
 
            cm.Parameters["@UserId"].Value = UserId;
 
            cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
 
            cm.Parameters["@UserKey"].Value = Key;
 
            cm.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
 
            cm.Parameters["@Result"].Direction = ParameterDirection.Output;
 
 
 
            cm.ExecuteNonQuery();
 
             
 
            if(cm.Parameters["@UserId"].Value.ToString().Trim()=="0")rStr = "success:false,err:'用户信息不存在!'";
 
            else rStr="success:true";
 
            cm.Dispose();
 
        }
 
        catch (Exception e)
 
        {
 
             
 
        }
 
        cn.Close();
 
        return rStr;
 
    }
 
 
 
    public static string Say()
 
    {
 
        HttpRequest Request = HttpContext.Current.Request;
 
        string From = Request.Form["from"], To = Request.Form["to"]
 
            , Key = Request.Form["key"], Msg = Request.Form["ct"], rStr = "";
 
        if (IsNull(From) || IsNull(Key) || IsNull(To) || IsNull(Msg)) rStr = "success:false,err:'信息传递不完整!'";
 
        else
 
        {
 
            SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
 
            cn.Open();
 
            try
 
            {               
 
                SqlCommand cm = new SqlCommand("ajaxSay", cn);
 
                cm.CommandType = CommandType.StoredProcedure;
 
                cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
 
                cm.Parameters["@UserKey"].Value = Key;
 
                cm.Parameters.Add(new SqlParameter("@From", SqlDbType.NVarChar, 18));
 
                cm.Parameters["@From"].Value = From;
 
                cm.Parameters.Add(new SqlParameter("@To", SqlDbType.NVarChar, 18));
 
                cm.Parameters["@To"].Value = To;
 
                cm.Parameters.Add(new SqlParameter("@Msg", SqlDbType.NVarChar, 800));
 
                cm.Parameters["@Msg"].Value = Msg;
 
                cm.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
 
                cm.Parameters["@Result"].Direction = ParameterDirection.Output;
 
                cm.ExecuteNonQuery();
 
                if (cm.Parameters["@Result"].Value.ToString() == "0") rStr = "sucess:false,err:'发表失败!\\n原因:接收者已经不存在!'";
 
                else rStr = "success:true";
 
                cm.Dispose();
 
            }
 
            catch (Exception e)
 
            {
 
                rStr = "sucess:false,err:'发表失败!原因\\n" + Js(e.Message) + "'";
 
            }
 
            cn.Close();
 
        }
 
        return rStr;
 
    }
 
 
 
    public static string ReadUser()
 
    {
 
        HttpRequest Request = HttpContext.Current.Request;
 
        string rStr = "", UserId = Request.Form["uid"];
 
        if (IsNull(UserId)) rStr += "success:false,err:'用户id丢失!'";
 
        else
 
        {
 
            SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
 
            cn.Open();
 
            try
 
            {
 
                SqlCommand cm = new SqlCommand("ajaxReadUser", cn);
 
                cm.CommandType = CommandType.StoredProcedure;
 
                cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
 
                cm.Parameters["@UserId"].Value = UserId;
 
                string j = "";
 
                SqlDataReader dr = cm.ExecuteReader();
 
                while (dr.Read()) j += ",{id:'" + dr[0] + "',nn:'" + Js(dr[1].ToString()) + "'}";
 
                dr.Close();
 
                cm.Dispose();
 
                rStr = "success:true,data:[" + (j == "" "" : j.Substring(1)) + "]";
 
            }
 
            catch (Exception e)
 
            {
 
                rStr = @"success:false,err:'发生如下错误\\n" + Js(e.Message) + "'";
 
            }
 
            cn.Close();
 
        }
 
        return rStr;
 
    }
 
 
 
    public static string Read()
 
    {
 
        HttpRequest Request = HttpContext.Current.Request;
 
        string rStr = "";
 
        string UserId = Request.Form["uid"], Key = Request.Form["key"];
 
        if (IsNull(UserId) || IsNull(Key)) rStr = "success:false,err:'用户信息丢失!'";
 
        else
 
        {
 
            SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
 
            cn.Open();
 
            try
 
            {
 
                SqlCommand cm = new SqlCommand("ajaxRead", cn);
 
                cm.CommandType = CommandType.StoredProcedure;
 
                cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
 
                cm.Parameters["@UserId"].Value = UserId;
 
                cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
 
                cm.Parameters["@UserKey"].Value = Key;
 
                SqlDataReader dr = cm.ExecuteReader();
 
                string j = "";
 
                while (dr.Read()) j += ",'" + Js(dr[0].ToString()) + "'";
 
                dr.Close();
 
                cm.Dispose();
 
                rStr = "success:true,data:[" + (j == "" "" : j.Substring(1)) + "]";
 
            }
 
            catch (Exception e)
 
            {
 
                rStr = "success:false,err:'发生以下错误" + Js(e.Message) + "'";
 
            }
 
            cn.Close();
 
        }
 
        return rStr;
 
    }
 
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...