用的是C#+ajax做无刷新聊天室,人数三四个的时候,没有什么问题,可是人一超过这个数,程序就出错,应该是出在读取数据的时候,有什么原因会造成这样的情况呢?各位做过聊天室的大佬能不能给说说啊,给点精验,会不会是和汉字的编码有关系?我是每隔1.5秒从mssql数据库中获取一次数据是我的罗辑有问题还是程序有问题呢?

解决方案 »

  1.   

    using System;
    using System.Data;
    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;
    using System.Data.SqlClient;public partial class GetOnlineUserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            clsPublic Public = new clsPublic();        if (Public.IsLogin())
            {
                //SqlCommand cmd = new SqlCommand();
                //SqlDataReader dr=null;
                try
                {
                    int intRID = Convert.ToInt32(Request.QueryString["RID"]);
                    SqlDataAdapter da = new SqlDataAdapter("Select fdUID,fdUserName,fdLevel From tb_Online Where fdRoomID=" + intRID + " And fdIsKickOut=0 Order By fdLevel Desc,fdLoginTime Asc",clsDatabase.getConn());
                    DataSet ds = new DataSet();
                    string strTemp="";
                    da.Fill(ds,"tbOnlineUser");
                    da.Dispose();
                    clsDatabase.CloseDB();
                    int intOnlineCount=ds.Tables["tbOnlineUser"].Rows.Count;
                    if(intOnlineCount>0){
                        for (int i = 0; i < intOnlineCount; i++)
                        {
                            int intUID = Convert.ToInt32(ds.Tables["tbOnlineUser"].Rows[i].ItemArray[0]);
                            string strUName = ds.Tables["tbOnlineUser"].Rows[i].ItemArray[1].ToString();
                            int intLevel = Convert.ToInt32(ds.Tables["tbOnlineUser"].Rows[i].ItemArray[2]);
                            switch (intLevel)
                            {
                                case 2:
                                    strTemp += "<span id=\"OnlineUser" + intUID + "\" class=\"RedTitle\" onMouseDown=\"UserAction(" + intUID + ",'" + strUName + "');\" title=\"给" + strUName + "发送消息\">" + strUName + "</span>(NJ)<br />";
                                    break;
                                case 1:
                                    strTemp += "<span id=\"OnlineUser" + intUID + "\" class=\"BlueTitle\" onMouseDown=\"UserAction(" + intUID + ",'" + strUName + "');\" title=\"给" + strUName + "发送消息\">" + strUName + "</span>(DB)<br />";
                                    break;
                                default:
                                    strTemp += "<span id=\"OnlineUser" + intUID + "\" class=\"BlackTitle\" onMouseDown=\"UserAction(" + intUID + ",'" + strUName + "');\" title=\"给" + strUName + "发送消息\">" + strUName + "</span><br />";
                                    break;
                            }
                        }
                    }
                    ds.Clear();
                    ds.Dispose();
                    Response.Write(strTemp);
                }
                catch (Exception err)
                {
                    Response.Write("系统提示:获取在线列表时出错"+err.Message+"<br />");
                }
                finally
                {
                    clsDatabase.CloseDB();
                }
            }
            else
            {
                Response.Write("系统提示:您尚未登录或登录信息已超时,请重新登录后进入聊天室。<br />");
            }
        }
    }
    这是其中获取在线用户的程序代码,也是出错的页面之一
      

  2.   

    这个是在线列表的出错提示:获取在线列表时出错Invalid cast from ‘DateTime‘ to ‘Int32‘.
    可我并没有读取DataTime类型的数据啊
      

  3.   

    这个是获取聊天信息时的出错信息:Index was outside the bounds of the array.
      

  4.   

    获取在线列表时出错Invalid cast from ‘DateTime‘ to ‘Int32‘.
    不要用TRY,看看程序出错在哪行。获取聊天信息时的出错信息:Index was outside the bounds of the array
    这个明显的告诉你了,数组越界了啊,最好也不用TRY,看看哪个数组出了问题
      

  5.   

    ds.Tables["tbOnlineUser"].Rows[i].ItemArray[0]
    ------------------
    看到底是得到什么值