三层架构第二课
        【摘要】 
                    
                        
                    
                    三层架构第二课 
数据表格的Cell_Click功能 
			txtDeptId.Enabled = false;
            btnUpdate.Enabled = true;
      ...
    
    
    
    三层架构第二课
数据表格的Cell_Click功能
			txtDeptId.Enabled = false;
            btnUpdate.Enabled = true;
            btnAdd.Enabled = false;
            btnDel.Enabled = true;
  
 - 1
- 2
- 3
- 4
单元格的行选中属性: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
- 2
- 3
- 4
- 5
一.编辑功能
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
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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();
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
三.复杂查询
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;
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3.1 刷新查询
public DataTable refreshData() {
            String sql = "select deptId 部门编号,deptName 部门名称 from dept";
            return DBHelper.query(sql,null);
        }
  
 - 1
- 2
- 3
- 4
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("类似于");
            }
        }
    }
}
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
3.2.2 BLL层
public class StudentService
    {
        StudentDao stuDao = new StudentDao();
        public DataTable queryByCondition(String context)
        {
            return stuDao.queryByCondition(context);
        }
    }
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
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;
        }
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3.2.4 界面演示
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/108935924
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)