三层架构第二课

数据表格的Cell_Click功能

			txtDeptId.Enabled = false;
            btnUpdate.Enabled = true;
            btnAdd.Enabled = false;
            btnDel.Enabled = true;

单元格的行选中属性:FullRowSelect

//单击的时候,选中项改变事件

       private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            txtDeptId.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtDeptName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        }

一.编辑功能

1.1 UI端

//填进去的值是最新的值
            dept = new Dept();
            dept.DeptId = txtDeptId.Text.Trim();
            dept.DeptName = txtDeptName.Text.Trim();

            bool result=deptService.updateDept(dept);
            if (result)
                MessageBox.Show("更新数据成功");
            else
                MessageBox.Show("更新数据失败");

            //调用查询方法;
            dataGridView1.DataSource = deptService.refreshData();

1.2

二.删除功能

2.1 UI端

 DialogResult dialog = MessageBox.Show("您确认是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialog == DialogResult.Yes) {
                dept = new Dept();
                dept.DeptId = txtDeptId.Text.Trim();

                bool result = deptService.delDept(dept);
                if (result)
                    MessageBox.Show("删除数据成功");
                else
                    MessageBox.Show("删除数据失败");
            }else{
            
            }
            //调用查询方法;
            dataGridView1.DataSource = deptService.refreshData();

三.复杂查询

3.0 通用查询

String url = "server=.;database=MyDb;uid=sa;pwd=123456";
            //1.链接对象;
            SqlConnection conn = new SqlConnection(url);
            conn.Open();
            //2.SqlCommand
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            if (parameters != null)
            {
                //继续添加参数;
                for (int i = 0; i < parameters.Count; i++)
                {
                    cmd.Parameters.Add(parameters[i]);
                }
            }
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt=new DataTable();
            da.Fill(dt);
            return dt;

3.1 刷新查询

public DataTable refreshData() {
            String sql = "select deptId 部门编号,deptName 部门名称 from dept";
            return DBHelper.query(sql,null);
        }

3.2 学生表的复杂查询

3.2.1 UI层

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;
using Model;
using BLL;

namespace UI
{
    public partial class frmStu : Form
    {
        //定义条件
        String currContext = "";
        StudentService stuService = new StudentService();

        public frmStu()
        {
            InitializeComponent();
        }

        private void frmStu_Load(object sender, EventArgs e)
        {
            //向条件下拉框,增加值;
            cmbSeaField.Items.Add("学号");
            cmbSeaField.Items.Add("姓名");
            cmbSeaField.Items.Add("年龄");
            cmbSeaField.Items.Add("性别");
            cmbSeaField.Items.Add("系部");
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            String searchField = "";
            if (cmbSeaField.SelectedItem==null) {
                MessageBox.Show("请选择查询项目");
                return;
            }
            //根据下拉框进行判断
            switch (cmbSeaField.SelectedItem.ToString()) { 
                case "学号":
                    searchField = "stuId";break;
                case "姓名":
                    searchField = "stuName"; break;
                case "年龄":
                    searchField = "age"; break;
                case "性别":
                    searchField = "gender"; break;
                case "系部":
                    searchField = "deptName"; break;
            }
            //条件判断,尽量用equals
            if (cmbOp.SelectedItem.ToString() != "类似于")
                currContext = searchField + cmbOp.SelectedItem.ToString() +  txtSeaContext.Text ;

            else
                currContext = searchField + " like '%" + txtSeaContext.Text + "%'";

            //调用查询
            bindGridView();
        }
        public void bindGridView() { 
            dgvStuList.DataSource=stuService.queryByCondition(currContext);
        }
        /// <summary>
        /// 选择框的更改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSeaField_SelectedIndexChanged(object sender, EventArgs e)
        {
            //当选项发生更改时,年轻是等值,其他是比较;
            cmbOp.Items.Clear();
            if (cmbSeaField.SelectedItem.ToString() == "年龄")
            {
                cmbOp.Items.Add(">");
                cmbOp.Items.Add(">=");
                cmbOp.Items.Add("=");
                cmbOp.Items.Add("<");
                cmbOp.Items.Add("<=");
            }
            else {
                cmbOp.Items.Add("=");
                cmbOp.Items.Add("类似于");
            }
        }
    }
}

3.2.2 BLL层

public class StudentService
    {
        StudentDao stuDao = new StudentDao();

        public DataTable queryByCondition(String context)
        {
            return stuDao.queryByCondition(context);
        }
    }

3.2.3 DAL层

这层代码需要修改之

public DataTable queryByCondition(String context)
        {
            String url = "server=.;database=MyDb;uid=sa;pwd=123456";
            //1.链接对象;
            SqlConnection conn = new SqlConnection(url);
            conn.Open();
            //2.SqlCommand
            SqlCommand cmd = conn.CreateCommand();
            String sql = "select * from student s left join dept d on s.deptId=d.deptId";
            if (context != "") {
                sql += " where " + context;
            }
            cmd.CommandText = sql;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

3.2.4 界面演示