C#窗体基于三层架构使用EntityFramework作为数据访问层,依据Code First原则【二】

本文介绍了如何在C#窗体应用中,基于三层架构和Entity Framework的Code First原则,实现数据访问层的搭建及运货商数据的增删查改功能。详细步骤包括新建窗体、BLL层方法、视图层事件处理,通过Lambda表达式进行模糊、精确和范围查询,并完成新增、修改和删除操作。
摘要由CSDN通过智能技术生成

创建项目,EF配置,数据访问层搭建请访问:

C#窗体基于三层架构使用EntityFramework作为数据访问层,依据Code First原则【一】

按教程完成后,我们继续。

一、在视图层新建窗体,命名test.cs

窗体布局如下:

二、在BLL层新建testManage.cs

继续添加运货商查询数据的方法

        EFHelper<运货商> nor = new EFHelper<运货商>();
        public List<运货商> GetList(string str)
        {
            return nor.getSearchList(item=>item.公司名称.Contains(str)).ToList();
        }

常用的Lambda表达式有以下:

//模糊查询运货商表公司名称列包含str的数据,相当于SQL的  %str%

nor.getSearchList(item => item.公司名称.Contains(str));

//查询运货商表公司名称列等于str的数据,相当于SQL的  运营商.公司名称==str
nor.getSearchList(item => item.公司名称.Equals(str));

//查询运货商表运货商ID列大于等于0的数据,相当于SQL的  运营商.运货商ID>=0(查询所有记录)
nor.getSearchList(item => item.运货商ID >= 0);


在视图层test.cs窗体文件中,编写代码

        testManage nor = new testManage();

        //查询按钮的点击事件
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string strInput = txtinput.Text;
            dataGridView1.DataSource = nor.GetList(strInput);
        }

using NorthwindCNShop.BLL;
using NorthwindCNShop.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NorthwindCNShop
{
    public partial class test : Form
    {
        testManage nor = new testManage();
        public test()
        {
            InitializeComponent();
        }

        private void test_Load(object sender, EventArgs e)
        {

        }
        //查询按钮的点击事件
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string strInput = txtinput.Text;
            dataGridView1.DataSource = nor.GetList(strInput);
        }
    }
}

查询功能就做出来了,运行test.cs窗体,点击查询按钮,查询数据库中的数据。

我们接着做增加功能,在BLL层testManege.cs文件中写增加的方法。

        public int DataAdd(运货商 param)
        {
            //增加成功返回1
            return nor.add(param);
        }

在视图层中的test.cs的“新增”按钮点击事件中编写以下代码

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //定义 运货商对象
            运货商 t = new 运货商();
            //输入框获取数据赋值给该对象
            t.公司名称 = txtName.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌药ice

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值