三层架构实现增删的简单实例
生活随笔
收集整理的這篇文章主要介紹了
三层架构实现增删的简单实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先建一個web.config的窗體,添加
<appSettings><add key="conn" value="server=.;database=test;integrated security=true"/></appSettings><connectionStrings/>字段
?
簡單頁面設計:
? ? ?
?
數據層 1 在DAL層創建連接: 2 SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"].ToString()); 3 4 5 public DataSet GetAll() 6 { 7 SqlDataAdapter da=new SqlDataAdapter(“select * from student_info ”,conn); 8 DataSet ds=new DataSet(); 9 da.Fill(ds); 10 return ds; 11 } 12 13 增加: 14 public void Insert(int id,int age,string name,string sex) 15 { 16 SqlCommand cmd=new SqlCommand("insert into student_info values("+id+","+age+",'"+name+"','"+sex+"')",conn); 17 conn.open(); 18 cmd.ExecuteNonQuery(); 19 conn.Close(); 20 } 21 刪除 22 public void Delete(int sid) 23 { 24 SqlCommand cmd=new SqlCommand("delete from student_infor where id="+sid+"",conn); 25 conn.open(); 26 cmd.ExecuteNonQuery(); 27 conn.Close(); 28 29 } 邏輯層 1 public class BLL_Class 2 { 3 private int id, age,sid; 4 private string name, sex; 5 public int ID 6 { 7 get { return id; } 8 set { id = value; } 9 } 10 public int Age 11 { 12 get { return age; } 13 set { age = value; } 14 } 15 public string Name 16 { 17 get { return name; } 18 set { name = value; } 19 } 20 public string Sex 21 { 22 get { return sex; } 23 set { sex = value; } 24 } 25 26 public int Sid 27 { 28 get { return sid; } 29 set { sid = value; } 30 } 31 DAL.DAl_Class DC = new DAL.DAl_Class(); 32 33 public DataSet Bind() 34 { 35 return DC.GetALL(); 36 } 37 public void Insert_info() 38 { 39 DC.Insert(ID, Age, Name, Sex); 40 } 41 public void Delete_info() 42 { 43 DC.Delete(Sid); 44 } 45 }?
顯示層 Bll.Bll_class BC;protected void Page_Load(object sender, EventArgs e) {BC = new BLL.BLL_Class();this.GridView1.DataSource = BC.Bind();this.GridView1.DataBind(); } //添加 protected void Button1_Click(object sender, EventArgs e) {BC.ID = Convert.ToInt32(TextBox1.Text.Trim().ToString());BC.Age = Convert.ToInt32(TextBox2.Text);BC.Name = this.TextBox4.Text;BC.Sex = this.TextBox3.Text;BC.Insert_info(); } 刪除 protected void Button2_Click(object sender, EventArgs e) {BC.Sid = Convert.ToInt32(TextBox1.Text.Trim().ToString());BC.Delete_info(); }?
轉載于:https://www.cnblogs.com/wynet/archive/2012/05/29/2523539.html
總結
以上是生活随笔為你收集整理的三层架构实现增删的简单实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用JDBC连接SQL Server数据
- 下一篇: notepad++默认的快捷键整理