在命名空间BussinessLogicLayer里的User方法代码如下:
using MyChatRoom.DataAccessLayer;
namespace MyChatRoom.BussinessLoginLayer
{
public class User
{
private string _userName;
private string _passWord;
private bool _Exist;
/// <summary>
/// 根据参数userName,获取用户详细信息
/// </summary>
public void LoadData(string userName)
{
Database db = new Database(); //实例化一个Database类
string Sql="select * from [User] where UserName='"+userName+"'";
DataRow dr = db.GetDataRow(Sql); //利用Database类里的GetDataRow方法查询数据库
if (dr != null)
{
this._userName = dr["UserName"].ToString();
this._passWord = dr["PassWord"].ToString();
this._Exist = true;
}
else
this._Exist = false;
}
public void Add(string userName, string passWord)
{
Database db = new Database(); //实例化一个Database类
string Sql = "insert [User] values('"+userName+"','"+passWord+"')";
db.ExecuteSQL(Sql); //利用Database类里的ExecuteSQL方法插入数据
}
}
用户界面里Button_onclick的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string passWord = txtPassWord.Text.Trim();
MyChatRoom.BussinessLoginLayer.User user = new User();
user.LoadData(userName); //获取用户信息
if (user.Exist)
{
Response.Redirect("Main.aspx");
}
} 这里有个错误 user.Exist //在实例化的user里找不到的
大侠教教我 要在User方法里怎么修改阿?
using MyChatRoom.DataAccessLayer;
namespace MyChatRoom.BussinessLoginLayer
{
public class User
{
private string _userName;
private string _passWord;
private bool _Exist;
/// <summary>
/// 根据参数userName,获取用户详细信息
/// </summary>
public void LoadData(string userName)
{
Database db = new Database(); //实例化一个Database类
string Sql="select * from [User] where UserName='"+userName+"'";
DataRow dr = db.GetDataRow(Sql); //利用Database类里的GetDataRow方法查询数据库
if (dr != null)
{
this._userName = dr["UserName"].ToString();
this._passWord = dr["PassWord"].ToString();
this._Exist = true;
}
else
this._Exist = false;
}
public void Add(string userName, string passWord)
{
Database db = new Database(); //实例化一个Database类
string Sql = "insert [User] values('"+userName+"','"+passWord+"')";
db.ExecuteSQL(Sql); //利用Database类里的ExecuteSQL方法插入数据
}
}
用户界面里Button_onclick的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string passWord = txtPassWord.Text.Trim();
MyChatRoom.BussinessLoginLayer.User user = new User();
user.LoadData(userName); //获取用户信息
if (user.Exist)
{
Response.Redirect("Main.aspx");
}
} 这里有个错误 user.Exist //在实例化的user里找不到的
大侠教教我 要在User方法里怎么修改阿?
if (user.Exist)
{
Response.Redirect("Main.aspx");
}
改为
if (user._Exist)
{
Response.Redirect("Main.aspx");
}
改为
if (this._Exist)
{
Response.Redirect("Main.aspx");
}
User user = new User();
1、把_Exist设置为属性;
private bool _Exist;
public bool Exist{
get{return _Exist;}
}
这样就可以user.Exist 了2、在LoadData方法里返回
如:
public bool LoadData(string userName)
{
Database db = new Database(); //实例化一个Database类
string Sql="select * from [User] where UserName='"+userName+"'";
DataRow dr = db.GetDataRow(Sql); //利用Database类里的GetDataRow方法查询数据库
if (dr != null)
{
this._userName = dr["UserName"].ToString();
this._passWord = dr["PassWord"].ToString();
return true;
}
else
return false;
} 你的代码改成:
if (user.LoadData(userName)) //获取用户信息
{
Response.Redirect("Main.aspx");
}
using MyChatRoom.DataAccessLayer;
namespace MyChatRoom.BussinessLoginLayer
{
public class User
{
private string _userName;
private string _passWord;
private bool _Exist;
/// <summary>
/// 根据参数userName,获取用户详细信息
/// </summary>
public void LoadData(string userName)
{
Database db = new Database(); //实例化一个Database类
string Sql="select * from [User] where UserName='"+userName+"'";
DataRow dr = db.GetDataRow(Sql); //利用Database类里的GetDataRow方法查询数据库
if (dr != null)
{
this._userName = dr["UserName"].ToString();
this._passWord = dr["PassWord"].ToString();
this._Exist = true;
}
else
this._Exist = false;
}
public bool Exist
{
get{return _Exist;}
set{_Exist= value;}
}
public void Add(string userName, string passWord)
{
Database db = new Database(); //实例化一个Database类
string Sql = "insert [User] values('"+userName+"','"+passWord+"')";
db.ExecuteSQL(Sql); //利用Database类里的ExecuteSQL方法插入数据
}
}
用户界面里Button_onclick的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string passWord = txtPassWord.Text.Trim();
MyChatRoom.BussinessLoginLayer.User user = new User();
user.LoadData(userName); //获取用户信息
if (user.Exist)
{
Response.Redirect("Main.aspx");
}
}
public bool Exist{
get(return this._Exist;}
}你以前做啥编程的啊,VB还是C啊?_这样的前缀命名规则真别扭啊
private string _passWord;
private bool _Exist; 这些是私有成员变量 你重构给类对应,加上属性就可以用 user.Exist
你的程序中并没有加Exist属性。只是设置了一个_Exist变量
在User类中加一个
Public bool Exist
{
get
{
return _Exist;
}
}
即可
public bool Exist
{
get
{
return this._exist;
}
}加上就能找到了。
{
set
{
this._userName=value;
}
get
{
return this._userName;
}
}
public string Password
{
set
{
this._password=value;
}
get
{
return this._password;
}
}
{
get
{
return this._exist;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string passWord = txtPassWord.Text.Trim();
MyChatRoom.BussinessLoginLayer.User user = new User();
user.LoadData(userName); //获取用户信息
if(user.Exist) //如果是老用户
{
if(user.Password==password) {
Response.Redirect("Main.aspx");
}
else {
Response.Write("<Script Language=JavaScript>alert(\"验证失败,请重新登录!\")</Script>");
}
}
}
最好再验证一下密码是否正确
{
get{ return _Exist; }
}
{
get{return _Exist;}
set{_Exist= value;}
}
但是前提是要把 private改成public 在button_click里才可以引用