三层常用就是UI,DAL,MODEL 还有其他的比如接口 模式等 这里以普通三层为例 实现插入数据的功能 。
先定义model实体类
View Code
private int uid;
private string uname;
private string upass;
public int UserId
{
get { return uid; }
set { uid = value; }
}
public string UserName
{
get { return uname; }
set { uname = value; }
}
public string UserPass
{
get { return upass; }
set { upass = value; }
}
DAL数据访问层;
public UserService() { } private string db = System.Configuration.ConfigurationSettings.AppSettings["ConstrSQL"].ToString();//数据库连接字符串 /// <summary> /// 执行数据插入 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Insert(Model.UserInfo model) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = db; conn.Open(); string strsql = "insert into UserInfos(UserName,UserPass)values(@uname,@upass)"; SqlCommand cmd = new SqlCommand(strsql, conn); cmd.Parameters.Add("@uname", SqlDbType.VarChar, 50); cmd.Parameters.Add("@upass", SqlDbType.VarChar, 50); cmd.Parameters["@uname"].Value = model.UserName; cmd.Parameters["@upass"].Value = model.UserPass; if (cmd.ExecuteNonQuery() > 0) { return true; } else { return false; } conn.Dispose(); conn.Close(); } }
BLL层我的理解就是UI与DAL之间的桥梁 ,主要是一调用的函数(方法)
View Code
public bool BLL_Insert(Model.UserInfo mu)
{
DAL.UserService du = new DAL.UserService();
return du.Insert(mu);
}
UI层调用BLL
View Code
1 Model.UserInfo mu = new Model.UserInfo();
2 mu.UserName = TextBox1.Text;
3 mu.UserPass = TextBox2.Text;
4 DAL.UserService du = new DAL.UserService();
5 du.Insert(mu);
6 Response.Write("提交成功!");
7 TextBox2.Text = "";
三层顺序model -dal-bll,BLL引用DAL,DAL引用MODEL