Home > Database > Mysql Tutorial > 三层架构(DAL层Data Access Layer数据库访问层)

三层架构(DAL层Data Access Layer数据库访问层)

WBOY
Release: 2016-06-07 15:11:26
Original
1826 people have browsed it

三层 架构 即是把 数据库 访问 ,业务逻辑,界面分离。 DAL常用封装:ToModel,ListAll(对于大数据量的数据不要提供,而是提供条件搜索),GetById,DeleteById,Update,Addnew. 再次使用上一实例的T_Student 数据库 第一步:新建一个类,命名为StudentDAL.cs

三层架构即是把数据库访问,业务逻辑,界面分离。

DAL常用封装:ToModel,ListAll(对于大数据量的数据不要提供,而是提供条件搜索),GetById,DeleteById,Update,Addnew.

再次使用上一实例的T_Student数据库

 第一步:新建一个类,命名为StudentDAL.cs将想要得到数据库数据总条数的方法(GetCount())创建到类中,代码如下:

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Text;

</span><span>namespace</span><span> ExecuteReader执行查询
{
    </span><span>class</span><span> StudentADL
    {
        </span><span>public</span> <span>static</span> <span>int</span><span> GetCount()
        {
            </span><span>return</span> (<span>int</span>)SqlHelper.ExecuteScalar(<span>"</span><span>select count(*) from T_Student</span><span>"</span><span>);
        }
    }
}</span>
Copy after login

第二步:新建一个窗体:MVC.AXML,并拖动一个按钮btnADL,并为其添加Click事件,代码如下:

<span>private</span> <span>void</span> btnADL_Click(<span>object</span><span> sender, RoutedEventArgs e)
        {
            MessageBox.Show(StudentADL.GetCount().ToString());
        }</span>
Copy after login

第三步:在StudentADL.cs中新建一个方法DeleteById()。代码如下:

 <span>public</span> <span>static</span> <span>void</span> DeleteById(<span>long</span><span> id)
        {
            SqlHelper.ExecuteNonQuery(</span><span>"</span><span>delete from T_Student where Id=@id</span><span>"</span>,<span>new</span> SqlParameter(<span>"</span><span>@id</span><span>"</span><span>,id));
           
        }</span>
Copy after login

第四步:在MVC.AXML,并拖动一个按钮btnDel,并为其添加Click事件,代码如下:

  <span>private</span> <span>void</span> btnDel_Click(<span>object</span><span> sender, RoutedEventArgs e)
        {
            StudentADL.DeleteById(</span><span>200</span><span>);
            MessageBox.Show(</span><span>"</span><span>成功删除</span><span>"</span><span>);
        }</span>
Copy after login

 

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template