标题:帮忙为代码做解释 大致啥意思?
取消只看楼主
天启圣元
Rank: 1
来 自:吉林长春
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-3-30
 问题点数:0 回复次数:0 
帮忙为代码做解释 大致啥意思?
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace UserManager
{
    public partial class Form1 : Form
    {
        string txtConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ykjj.mdb";
        public Form1()
        {
            InitializeComponent();
        }

        private void LoadData(string name)
        {
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                string sql;
                if (name.Trim() != "")
                    sql = String.Format("select yhm as 用户名,mm as 密码,qx as 权限 from czyb where yhm like'%{0}%'", name);
                else
                    sql = "select yhm as 用户名,mm as 密码,qx as 权限 from czyb";
                DataSet ds = new DataSet();
                OleDbDataAdapter da= new OleDbDataAdapter(sql,conn);
                da.Fill(ds, "user");
                bdsUser.DataMember = "user";
                bdsUser.DataSource = ds;
                dgvUser.DataSource = bdsUser;
                bdnUser.BindingSource = bdsUser;
            }
            //dgvUser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {
                     
            //下面的代码用于ComboBox数据绑定
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                string sql = "select distinct qx from czyb";
                DataSet ds = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
                da.Fill(ds, "qxb");
                bdsQx.DataMember = "qxb";
                bdsQx.DataSource = ds;
                cmbPriority.ValueMember = "qx";
                cmbPriority.DisplayMember = "qx";
                cmbPriority.DataSource = bdsQx;
            }
            LoadData("");
            txtName.DataBindings.Add("Text", bdsUser, "用户名");
            txtPassword.DataBindings.Add("Text", bdsUser, "密码");
            cmbPriority.DataBindings.Add("SelectedValue", bdsUser, "权限");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(txtSearch.Text);
            
        }
        private bool CheckTrim(string name,string password,string priority)
        {
            if (name == "" || password == "" ||priority == "")
            {               
                return true;
            }
            else
                return false;
        }
        private bool Check(string yhm)   //判断该用户是否已存在
        {
            int i=0;
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                conn.Open();
                string sql = String.Format("select count(*) from czyb where yhm='{0}'",yhm);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
               
                try
                {
                    i=Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                conn.Close();
            }
            if (i > 0) return true;
            else return false;
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string password = txtPassword.Text.Trim();
            string priority = cmbPriority.Text.Trim();
            if (CheckTrim(name, password, priority))
            {
                MessageBox.Show("用户信息不能为空");
                return;
            }
            if (Check(name))
            {
                MessageBox.Show("该用户名已存在!");
                return;
            }
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                conn.Open();
                string sql = String.Format("insert into czyb(yhm,mm,qx) values('{0}','{1}','{2}')", name, password, priority);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                conn.Close();
            }
            LoadData(txtSearch.Text);
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string password = txtPassword.Text.Trim();
            string priority = cmbPriority.Text.Trim();
            if (CheckTrim(name, password, priority))
            {
                MessageBox.Show("用户信息不能为空");
                return;
            }
           
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                conn.Open();
                string sql = String.Format("update czyb set mm='{0}',qx='{1}' where yhm='{2}'", password, priority, name);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("修改记录成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                conn.Close();
            }
            LoadData(txtSearch.Text);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            string name = dgvUser.CurrentRow.Cells[0].Value.ToString();
            using (OleDbConnection conn = new OleDbConnection(txtConn))
            {
                conn.Open();
                string sql = String.Format("delete from czyb where yhm='{0}'", name);
        
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                if(MessageBox.Show("确认要删除吗?","警告!",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
                {
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    conn.Close();
                }
            }
            LoadData(txtSearch.Text);
        }

        private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //txtName.Text = dgvUser.CurrentRow.Cells[0].Value.ToString();
            //txtPassword.Text = dgvUser.CurrentRow.Cells[1].Value.ToString();
            //cmbPriority.Text = dgvUser.CurrentRow.Cells[2].Value.ToString();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                btnSearch.PerformClick();
        }

        private void dgvUser_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}
搜索更多相关主题的帖子: private public 
2017-04-17 14:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-476193-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.333580 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved