• 三层架构实现增删的简单实例


    首先建一个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();
    }
  • 相关阅读:
    RIFF格式简介
    获取控制台窗口句柄GetConsoleWindow
    控制台编程基础总结
    控制台访问权限、别名及注意事项
    控制台输入输出机制实例
    控制台输入输出机制
    控制台基础概念实例
    STL之std::set、std::map的lower_bound和upper_bound函数使用说明
    wcout输出中文不显示
    控制台基础概念
  • 原文地址:https://www.cnblogs.com/wynet/p/2523539.html
Copyright © 2020-2023  润新知