上次的登陆没写完,写了个未完待续,结果一直没续上,这次写个完整的,再不写就忘了。
登陆一开始弄是觉得很麻烦,什么错误都有,不懂,但是弄过之后就觉得还是登陆最简单。下面就介绍一下 我写的登陆,其实呢跟其他人的都大同小异,都是这个路子,都是走一遍七层。之后的都是在这个路子上添加东西,这个登陆的代码还是最基础的,可以多理解理解。
UI层
private void button1_Click(object sender, EventArgs e)
{
if (txtpassword.Text.Trim() == "" || txtusername.Text=="")
{
MessageBox.Show("请填写完整信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Entity.user user = new Entity.user();
user.UserName = txtusername.Text.Trim();
user.PWD = txtpassword.Text;
userids = txtusername.Text.Trim();
Facade.Facade loginfacade = new Facade.Facade(); //实例化登陆外观将参数传递给外观层
DataTable flag = loginfacade.selectuser(user);
if (flag.Rows.Count!=0)
{
MessageBox.Show("登陆成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//登陆成功提示信息
Form2 form2 = new Form2();
; form2.Show();
}
else
{
MessageBox.Show("您输入的信息有错误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtusername.Text = "";
txtpassword.Text = "";
txtusername.Focus();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Environment.Exit(0); //最干净的退出方式
}
BLL层
public class loginBLL
{
public DataTable UserBLL(Entity.user user)
{
Factory.Factory fact = new Factory.Factory();//实例化工厂
IDAL.IDAL idal = fact.CreateUser();//调用工厂方法创建接口
DataTable table = idal.selectUser(user);//接受D层的返回值
return table;
}
}
DAL层
public class LoginDAL : IDAL.IDAL
{
public DataTable selectUser(Entity.user User)
{
SQLHelper sqlHelper = new SQLHelper();
SqlParameter[] sqlParams = { new SqlParameter("@userID", User.UserName), new SqlParameter("@PassWord", User.PWD) };
string sql = @"SELECT * FROM User_Info WHERE userID=@userID AND PWD=@PassWord";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;
}
}
Entity(实体)层
public class user
{
public string userID { get; set; }
public string level { get; set; }
public string UserName { get; set; }
public string PWD { get; set; }
}
Facade(外观层)
public class Facade
{
public DataTable selectuser(Entity.user user)
{
DataTable flag;
loginBLL.loginBLL userBLL = new loginBLL.loginBLL();
flag = userBLL.UserBLL(user);
return flag;
}
}
IDAL(接口层)
public interface IDAL
{
DataTable selectUser(Entity.user userinfo);
}
Facorty(工厂层)
public class Factory
{
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];//接受来自配置文件的数据
public IDAL.IDAL CreateUser()
{
string className = StrDB + "." + "LoginDAL";//DAL层的类名
IDAL.IDAL idal = (IDAL.IDAL)Assembly.Load(StrDB).CreateInstance(className);//反射加工厂的应用
return idal;
}
}
这就是这七层的框架,是基础!当然要完成登陆光有这七层是不够的,还要有app.config配置文件和sqlHelper,都写完了就可以完成登陆了,对于这两个我就不写了,为什么呢?因为快放学了,写不完了!有机会下次再补上吧。