⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formyhxx.cs

📁 超市进销存管理系统 Visual Studio 2005+SQL Sever2005
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _10901BS
{
    public partial class formYhxx : Form
    {

        int c = 0;
        SqlConnection cn = new SqlConnection();
        SqlCommand cm = new SqlCommand();
        SqlDataAdapter da;
        
        SqlDataReader ccc;
        DataSet cc = new DataSet();

        public formYhxx()
        {
            InitializeComponent();
        }

        private void yhxxBiao_Load(object sender, EventArgs e)
        {
            if (cn.State == ConnectionState.Closed)
            {
                cn.ConnectionString = "Data Source=.;Initial Catalog=110901DB;Integrated Security=True";
                cn.Open();
            }
            this.cbYgID.Items.Clear();
            DataSet cc1 = new DataSet();
                    da = new SqlDataAdapter("select ygID from ygBiao ", cn);
                    da.Fill(cc1);

                    foreach (DataRow row in cc1.Tables[0].Rows)
                    {
                        this.cbYgID.Items.Add(row.ItemArray[0]);

                    }

            da = new SqlDataAdapter("select yhID as '用户名',yhName as '员工编号',useAb as '登陆权限',yhPsd from yhxxBiao", cn);
            cc.Clear();
            da.Fill(cc);
            this.dataGridView1.DataSource = cc.Tables[0];
            this.dataGridView1.Columns[0].ReadOnly = true;
            this.dataGridView1.Columns[1].ReadOnly = true;
            this.dataGridView1.Columns[3].Visible = false;
            this.txtyhID.Text = "";
            this.cbYgID.Text = "";
            this.txtyhPSD.Text = "";
            this.txtuseAb.Text = "";
            
        }

        //刷新功能
        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            yhxxBiao_Load(sender, e);
        }

        //查询功能
        private void 查询_Click(object sender, EventArgs e)
        {

            if (txtyhPSD.Text != "")
            {
                MessageBox.Show("对不起,不能查询此信息!");
                txtyhID.Focus();
                return;
            }
            if (txtyhID.Text == "" && this.cbYgID.Text == "" )
            {
                MessageBox.Show("请输入查询信息!");
                return;
            }
            //yhID查询
            else 
            {
                cm = new SqlCommand();
                cm.Connection = cn;
                cm.CommandText = "select * from yhxxBiao where yhID like '%" + this.txtyhID.Text + "%' and yhName like '%" + this.cbYgID.Text + "%' ";
                ccc = cm.ExecuteReader();
                if (ccc.Read())
                {
                    cc.Clear();
                    ccc.Close();
                    da = new SqlDataAdapter("select yhID as '用户名',yhName as '员工编号',useAb as '登陆权限',yhPsd from yhxxBiao where yhID like '%" + this.txtyhID.Text + "%' and yhName like '%" + this.cbYgID.Text + "%' ", cn);
                    da.Fill(cc);
                    MessageBox.Show("查询成功!");
                    this.dataGridView1.DataSource = cc.Tables[0];
                    this.dataGridView1.Columns[3].Visible = false;
                }
                else
                {
                    ccc.Close();
                    MessageBox.Show("系统提示:您查询的信息不存在!");
                }
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txtyhID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            this.cbYgID.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            this.checkBox1.Checked = Convert.ToBoolean(this.dataGridView1.CurrentRow.Cells[2].Value);
            this.txtyhPSD.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();

        }

        private void txtyhID_KeyPress(object sender, KeyPressEventArgs e)
        {

            //用循环的方式禁用全角字符
            char[] cc ={ '~', '!', '·', '#', '¥', '%', '…', '—', '*', '(', ')', '《', '》', '?', '。', ',', '“', ':', ';', '‘' };

            foreach (char c in cc)
            {
                if (e.KeyChar == c)
                    e.Handled = true;
            }

        }

        private void txtyhName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( e.KeyChar != 8 || e.KeyChar != 13)
            {
                e.Handled = true;
            }

        }
        //添加功能
        private void tslTj_Click(object sender, EventArgs e)
        {
            if (this.txtyhPSD.Text == "")
            {
                 MessageBox.Show("系统提示:您尚未设置密码,请重新输入!");
                 this.txtyhPSD.Focus();
                 return;
            }
            cm.Connection = cn;
            cm.CommandText = "select * from yhxxBiao where yhID like '%" + this.txtyhID.Text + "%'";
            ccc = cm.ExecuteReader();
            if (ccc.Read())
            {
                ccc.Close();
                MessageBox.Show("系统提示:您不能添加重复的用户名,请重新输入!");
                this.txtyhID.Text = "";
                this.txtyhID.Focus();
                return;
            } 
            ccc.Close();
            cm.Connection = cn;
            cm.CommandText = "select * from yhxxBiao where yhName like '%" + this.cbYgID.Text + "%' ";
            ccc = cm.ExecuteReader();
            if (ccc.Read())
            {
                ccc.Close();
                MessageBox.Show("系统提示:您不能添加重复的员工编号,请重新输入!");
                this.cbYgID.Text = "";
                this.cbYgID.Focus();
                return;
            }
            ccc.Close();


            cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandText = "insert yhxxBiao values('" + this.txtyhID.Text + "','" + this.cbYgID.Text + "','" + this.txtyhPSD.Text + "','" + this.checkBox1.Checked.ToString() + "')";
            cm.ExecuteNonQuery();
            MessageBox.Show("添加成功!");
            yhxxBiao_Load(sender, e);
        }
        //修改功能
        private void tslXg_Click(object sender, EventArgs e)
        {

            cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandText = "update yhxxBiao set yhPSD='" + this.txtyhPSD.Text + "',useAb='" + this.checkBox1.Checked.ToString() + "' where yhID='" + this.txtyhID.Text + "' "; 
            cm.ExecuteNonQuery();
            MessageBox.Show("修改成功!");
            yhxxBiao_Load(sender, e);

        }
        //删除功能
        private void tslSc_Click(object sender, EventArgs e)
        {
            if (this.txtyhID.Text == "0001")
            {
                MessageBox.Show("对不起此用户的信息不能被删除!");
                return;
            }

            if (c == 0)
            {
                DialogResult f = MessageBox.Show("真的要删除吗!", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (f == DialogResult.Yes)
                {
                    cm = new SqlCommand();
                    cm.Connection = cn;
                    cm.CommandText = "delete yhxxBiao where yhID='" + this.txtyhID.Text + "'";
                    cm.ExecuteNonQuery();
                    MessageBox.Show("删除成功!");
                    yhxxBiao_Load(sender, e);

                }
            }
            else
            {
                MessageBox.Show("您的操作未完成,请点击 取消 后进行此操作!");
            }
        }

        private void txtyhID_Leave(object sender, EventArgs e)
        {
            cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandText = "select yhName from yhxxBiao where yhID='" + this.txtyhID.Text + "' ";
            ccc = cm.ExecuteReader();
            if (ccc.Read())
            {
                ccc.Close();
                this.cbYgID.Text = cm.ExecuteScalar().ToString();
            }
            ccc.Close();
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -