您的位置:首页 > 运维架构 > 网站架构

c#多层架构

2011-10-30 19:29 162 查看
三层结构:表现层,业务逻辑层,数据访问层。

功能:

表现层:数据的现实和接收用户输入的数据;(为用户提供一种交互式操作的界面)

业务逻辑层:处理数据;它处于表现层与数据访问层之间,起到了数据交互中承上启下的作用。

数据访问层(持久层):实现了对数据的保存和读取操作。它还负责想业务逻辑层提供数据和修改数据的操作。可以提高数据访问的安全性。简单说:就是实现数据库的增删改查操作。

*表层不能直接访问数据访问层。

不然三层就没有意义失去了高聚合,低耦合的设计思想。

三层的好处:

1.可以只关注整个结构中的某一层;

2.可以很容易的用新的实现来替换原有层次实现。

3可以降低层与层之间的依赖。

4有利于标准化

5利于各层逻辑的复用。

缺点:降低了系统的性能,如果不采用分层结构,很多业务可以直接访问数据库,以此获取相应的数据,如今却必须通过中间层来完成。

2有时会导致级联的修改,如在表层中需要增加一个功能,为保证其设计符合分层式结构,可能需要在相应的业务逻辑层和数据访问层中增加相应的代码。

namespace DAL

{

public class UserInfoDAL

{

DataSet ds = new DataSet();

public bool AddUser(int age,string name,string address,string sex)

{

bool res = false;

SqlConnection sqlcon = new SqlConnection("server=.;uid=ad;pwd=123;database=MySchool");

try

{

sqlcon.Open();

string sql = "insert into userinfo1(username,age,sex,address) values('"+name +"',"+age +",'"+sex+"','"+address+"')";

SqlCommand sqlcom = new SqlCommand(sql,sqlcon);

if (sqlcom.ExecuteNonQuery() > 0)

{

res = true;

}

sqlcon.Close();

}

catch (Exception)

{

throw;

}

return res;

}

public DataSet GetAllUser()

{

SqlConnection sqlcon = new SqlConnection("server=.;uid=ad;pwd=123;database=myschool");

try

{

sqlcon.Open();

string sql = "select * from userinfo1";

SqlDataAdapter sa = new SqlDataAdapter(sql,sqlcon);

sa.Fill(ds,"alluser");

sqlcon.Close();

}

catch (Exception)

{

throw;

}

return ds ;

}

public DataSet GetUserByName(string name)

{

SqlConnection sqlcon = new SqlConnection("server=.;uid=ad;pwd=123;database=myschool");

try

{

sqlcon.Open();

string sql = "select * from userinfo1 where UserName='"+name+"'";

SqlDataAdapter sa = new SqlDataAdapter(sql,sqlcon);

sa.Fill(ds,"alluser");

sqlcon.Close();

}

catch (Exception)

{

throw;

}

return ds;

}

public DataSet GetUserByAge(int age)

{

SqlConnection sqlcon = new SqlConnection("server=.;uid=ad;pwd=123;database=myschool");

try

{

sqlcon.Open();

string sql = "select * from userinfo1 where age='" + age + "'";

SqlDataAdapter sa = new SqlDataAdapter(sql, sqlcon);

sa.Fill(ds, "alluser");

sqlcon.Close();

}

catch (Exception)

{

throw;

}

return ds;

}

public DataSet GetUserByAddress(string address)

{

SqlConnection sqlcon = new SqlConnection("server=.;uid=ad;pwd=123;database=myschool");

try

{

sqlcon.Open();

string sql = "select * from userinfo1 where address='" +address + "'";

SqlDataAdapter sa = new SqlDataAdapter(sql, sqlcon);

sa.Fill(ds, "alluser");

sqlcon.Close();

}

catch (Exception)

{

throw;

}

return ds;

}

}

}

namespace BLL

{

public class UserInfoBLL

{

public bool AddUser(int age, string name, string address, string sex)

{

UserInfoDAL ud = new UserInfoDAL();

return ud.AddUser(age,name,address,sex );

}

public DataSet GetAllUser()

{

UserInfoDAL ud = new UserInfoDAL();

return ud.GetAllUser();

}

public DataSet GetUserByType(string type,string mes)

{

DataSet ds = new DataSet();

UserInfoDAL ud = new UserInfoDAL();

switch (type)

{

case "姓名":

ds=ud.GetUserByName(mes);

break;

case "年龄":

ds = ud.GetUserByAge (Convert .ToInt32(mes));

break;

case "住址":

ds = ud.GetUserByAddress (mes);

break;

}

return ds;

}

}

}

namespace UI

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

UserInfoBLL ub = new UserInfoBLL();

bool res = ub.AddUser(Convert.ToInt32(tb_age.Text), tb_name.Text, tb_address.Text, (rb_nan.Checked ? '男' : '女').ToString());

if (res)

{

MessageBox.Show("成功");

}

else

{

MessageBox.Show("失败");

}

}

private void 搜_Click(object sender, EventArgs e)

{

UserInfoBLL ub = new UserInfoBLL();

string type = "";

foreach (Control var in panel1.Controls)

{

if (var is RadioButton)

{

RadioButton r = (RadioButton)var;

if (r.Checked)

{

type = r.Text;

}

}

}

dataGridView1.DataSource = ub.GetUserByType (type,textBox1.Text).Tables["alluser"];

}

private void 全部_Click(object sender, EventArgs e)

{

UserInfoBLL ub = new UserInfoBLL();

dataGridView1.DataSource = ub.GetAllUser().Tables["alluser"];

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: