第一步:首先创建一个Models类库(解决方案上)
1.使用面向对象的方法封装一个UserInfo类
public class UserInfo
{
public UserInfo()
{ }
public UserInfo(int id, string uid, string pwd, string name, int age, string gender)
{
this.id = id;
this.uid = uid;
this.pwd = pwd;
this.name = name;
this.age = age;
this.gender = gender;
}
public UserInfo( string uid, string pwd, string name, int age, string gender)
{
this.uid = uid;
this.pwd = pwd;
this.name = name;
this.age = age;
this.gender = gender;
}
public int id { get; set; }
public string uid { get; set; }
public string pwd { get; set; }
public string name { get; set; }
public int age{ get; set; }
public string gender { get; set; }
}
第二步:创建我们的 “打工仔” DAL 类库,将它与我们的Models层关联(展开选项,右击引用,添加引用)(解决方案上)
1.可爱的DBHelper登场(超超超简单版~)
public class DBHelper
{
//连接对象
public static SqlConnection con = new SqlConnection("server=.;database=DB2003;uid=sa;pwd=123");
//判断连接是否打开
public static void OpenOrClose()
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
//增删改
public static bool Execute(string sql)
{
OpenOrClose();
SqlCommand cmd = new SqlCommand(sql, con);
int count = cmd.ExecuteNonQuery();//增删改方法
OpenOrClose();
return count > 0;
}
//非断开式查询
public static SqlDataReader GetReader(string sql)
{
OpenOrClose();
SqlCommand cmd = new SqlCommand(sql, con);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//断开式查询
public static DataTable GetTable(string sql)
{
OpenOrClose();
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
sda.Fill(dt);
OpenOrClose();
return dt;
}
}
2.创建UserInfoDAL类
public class UserInfoDAL
{
//使用非断开式查询
public SqlDataReader GetReader(string uid, string pwd)
{
string sql = string.Format("SELECT * FROM user_info where uid='{0}' and pwd='{1}'", uid, pwd);
return DBHelper.GetReader(sql);
}
//新增用户信息
public bool AddUser(UserInfo user)
{
string sql = string.Format("insert into user_info values('{0}','{1}','{2}','{3}','{4}')"
,user.uid,user.pwd,user.name,user.age,user.gender);
return DBHelper.Execute(sql);
}
}
第三步:创建我们的 “CEO” BLL类库(解决方案上),将它与我们的Models、DAL层关联
1.同样,创建一个UserInfoBLL类
public class UserInfoBLL
{
//调用UserInfoDAL的方法
UserInfoDAL dal = new UserInfoDAL();
public UserInfo Login(string uid, string pwd)
{
SqlDataReader reader= dal.GetReader(uid ,pwd);
UserInfo user = null;
//判断SqlDataReader中是否有数据
if (reader.Read())
{
user = new UserInfo();
user.id = reader.GetInt32(0);
user.uid = reader.GetString(1);
user.pwd = reader.GetString(2);
user.name = reader.GetString(3);
user.age = reader.GetInt32(4);
user.gender = reader.GetString(5);
}
DBHelper.OpenOrClose();
return user;
}
//用户注册,
public bool reg(string uid, string pwd, string name, int age, string gender)
{
return dal.AddUser(new UserInfo(uid, pwd, name, age, gender));
}
}
第四步:创建web窗体(项目上)将它与我们的Models、BLL层关联
1.1.创建登录页面
1.2.登录页面后台代码
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string uid = this.TextBox1.Text;
string pwd = this.TextBox2.Text;
UserInfoBLL bll = new UserInfoBLL();
//调用BLL层的Login方法,返回用户对象
UserInfo user= bll.Login(uid,pwd);
//根据用户对象是否为空,来判断登录操作是否成功
if (user != null)
{
Session["user"] = user;
Response.Redirect("Index.aspx");
}
else
{
Response.Write("");
}
}
}
2.1.创建注册页面
2.2.注册页面后台代码
public partial class Reg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
UserInfoBLL bll = new UserInfoBLL();
if (bll.reg(txtuid.Text, txtpwd.Text, txtname.Text, int.Parse(txtage.Text), DropDownList1.Text))
{
Response.Write("");
}
else
{
Response.Write("");
}
}
}
OK!大功告成!最后提示一下,如果要引用其他层的东西,记得alt + enter 一下,选择using 那个选项。