1,注意是否引用了相应的名称空间 using System.Web; using System.Web.SessionState; 2,注意Session的大小写。
using System; using System.Data; using System.Data.Odbc; using System.Collections; using System.Configuration; using System.Web; using System.Web.SessionState; public int Qoperator(string sUserName,string sPassword,string spwds) { ArrayList alID = new ArrayList(); ArrayList alName= new ArrayList(); ArrayList alPassword = new ArrayList(); ArrayList alCalcType = new ArrayList(); ArrayList alTeam = new ArrayList(); alID.Clear(); alName.Clear(); alPassword.Clear(); alCalcType.Clear(); alTeam.Clear(); alID.Add(0); alName.Add("webmaster"); alPassword.Add("coagent"); alCalcType.Add("0");//不加密 alTeam.Add(0);
OdbcConnection con = new OdbcConnection(ConfigurationSettings.AppSettings["sODBC"]); OdbcCommand cmd; OdbcDataReader reader; con.Open(); cmd = con.CreateCommand(); int i=1; cmd.CommandText = "SELECT id,rtrim(name), rtrim(password),team FROM ext_user"; reader = cmd.ExecuteReader(); while (reader.Read()) { if (!reader.IsDBNull(0)) { alID.Add(reader.GetDecimal(0)); if (reader.GetDecimal(0) > i) i = int.Parse(reader.GetDecimal(0).ToString())+1; alName.Add(reader.GetString(1)); } if (!reader.IsDBNull(2)) alPassword.Add(reader.GetString(2)); else alPassword.Add(""); alCalcType.Add("HeNanFuKong"); alTeam.Add(reader.GetDecimal(3)); } reader.Close(); cmd.CommandText = "SELECT opername, password,id FROM ext_operator_fk"; reader = cmd.ExecuteReader(); while (reader.Read()) { if (!reader.IsDBNull(0)) { alID.Add(i++); alName.Add(reader.GetString(0)); alPassword.Add(reader.GetString(1)); alCalcType.Add("HeNanFuKong"); if (reader.GetDecimal(2) ==0)//负控管理员 alTeam.Add(2); else alTeam.Add(3); } } reader.Close(); con.Close(); int nID = -1; for (int j=0; j<alID.Count; j++) //比较用户名和密码 { if (sUserName == (string)alName[j]) { switch (alCalcType[j].ToString()) { case "0": if (sPassword == (string)alPassword[j]) nID =j; break; case "HeNanFuKong": if (spwds == (string)alPassword[j]) nID = j; break; } } if (nID!=-1) break; } if (nID != -1) { Session["UserID"] = alID[nID]; Session["UserName"] = alName[nID]; Session["UserTeam"] = alTeam[nID];
using System.Web;
using System.Web.SessionState;
2,注意Session的大小写。
using System.Data;
using System.Data.Odbc;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.SessionState;
public int Qoperator(string sUserName,string sPassword,string spwds)
{
ArrayList alID = new ArrayList();
ArrayList alName= new ArrayList();
ArrayList alPassword = new ArrayList();
ArrayList alCalcType = new ArrayList();
ArrayList alTeam = new ArrayList(); alID.Clear();
alName.Clear();
alPassword.Clear();
alCalcType.Clear();
alTeam.Clear(); alID.Add(0);
alName.Add("webmaster");
alPassword.Add("coagent");
alCalcType.Add("0");//不加密
alTeam.Add(0);
OdbcConnection con = new OdbcConnection(ConfigurationSettings.AppSettings["sODBC"]);
OdbcCommand cmd;
OdbcDataReader reader;
con.Open();
cmd = con.CreateCommand(); int i=1;
cmd.CommandText = "SELECT id,rtrim(name), rtrim(password),team FROM ext_user";
reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
alID.Add(reader.GetDecimal(0));
if (reader.GetDecimal(0) > i)
i = int.Parse(reader.GetDecimal(0).ToString())+1;
alName.Add(reader.GetString(1));
}
if (!reader.IsDBNull(2))
alPassword.Add(reader.GetString(2));
else
alPassword.Add("");
alCalcType.Add("HeNanFuKong");
alTeam.Add(reader.GetDecimal(3));
}
reader.Close(); cmd.CommandText = "SELECT opername, password,id FROM ext_operator_fk";
reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
alID.Add(i++);
alName.Add(reader.GetString(0));
alPassword.Add(reader.GetString(1));
alCalcType.Add("HeNanFuKong");
if (reader.GetDecimal(2) ==0)//负控管理员
alTeam.Add(2);
else
alTeam.Add(3);
}
}
reader.Close();
con.Close();
int nID = -1;
for (int j=0; j<alID.Count; j++) //比较用户名和密码
{
if (sUserName == (string)alName[j])
{
switch (alCalcType[j].ToString())
{
case "0":
if (sPassword == (string)alPassword[j])
nID =j;
break;
case "HeNanFuKong":
if (spwds == (string)alPassword[j])
nID = j;
break;
}
}
if (nID!=-1)
break;
}
if (nID != -1)
{
Session["UserID"] = alID[nID];
Session["UserName"] = alName[nID];
Session["UserTeam"] = alTeam[nID];
}
return nID;
}
System.Web.HttpContext.Current.Session["UserID"] = alID[nID];或者,把System.Web.HttpContext.Current传递到当前的方法中:
public int Qoperator(string sUserName,string sPassword,string spwds,System.Web.HttpContext.Current cur)cur.Session["UserID"] = alID[nID];
那么你的这段可以写在
IList aluser=new ArrayList();
..
while(reader.Read())
{
alUser al=new alUser(reader.GetInt32(..),reader.Get..)
aluser.Add(al);
}
..
最后赋值给Session时也可以直接将一个alUser对象的实例赋给Session,操作起来方便太多了。
这牵涉到编程的理念的问题,Session是一个持久化的容器。理解这一点比较难,所以在这里我也不想多说,多做项目以后自然能理解。