C# 功能完整的单表增删改查程序

id字段自增,标识增量和种子1;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace mpmng
{
    public partial class Form1 : Form
    {
        string MyConn = "server=localhost;uid=sa;pwd=xx123xx;database=dxp;";
        SqlConnection MyConnection = null;
        DataSet ds1 = new DataSet();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection MyConnection = new SqlConnection(MyConn);
            SqlCommand MyCommand = new SqlCommand("SELECT * FROM view_emp", MyConnection);
            SqlDataAdapter SelectAdapter = new SqlDataAdapter();
            SelectAdapter.SelectCommand = MyCommand;
            DataSet ds = new DataSet();
            MyConnection.Open();
            SelectAdapter.SelectCommand.ExecuteNonQuery();
            
            SelectAdapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Columns[0].Width /= 3;

            comboBox1.Items.Add("男");
            comboBox1.Items.Add("女");
            comboBox1.Text = "";

            SqlCommand MyCommand2 = new SqlCommand("SELECT distinct 部门 FROM view_emp", MyConnection);
            SqlDataAdapter SelectAdapter2 = new SqlDataAdapter();
            SelectAdapter2.SelectCommand = MyCommand2;
            DataSet ds2 = new DataSet();
            SelectAdapter2.SelectCommand.ExecuteNonQuery();
            MyConnection.Close();
            SelectAdapter2.Fill(ds2);
            for(int i=0;i<ds2.Tables[0].Rows.Count;i++)
            {
                comboBox2.Items.Add(ds2.Tables[0].Rows[i][0].ToString());
            }

        }

        private void button1_Click(object sender, EventArgs e)//姓名查询
        {
            SqlConnection MyConnection = new SqlConnection(MyConn);
            string sql1 = "SELECT * FROM view_emp where 姓名 like '%" + textBox1.Text + "%'";
            SqlCommand MyCommand = new SqlCommand(sql1, MyConnection);
            SqlDataAdapter SelectAdapter = new SqlDataAdapter();
            SelectAdapter.SelectCommand = MyCommand;
            DataSet ds = new DataSet();
            MyConnection.Open();
            SelectAdapter.SelectCommand.ExecuteNonQuery();
            MyConnection.Close();
            SelectAdapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new mpmng.Form2();
            f2.ShowDialog();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection MyConnection = new SqlConnection(MyConn);
            string sql1 = "SELECT * FROM view_emp where 性别 = '" + comboBox1.Text + "'";
            SqlCommand MyCommand = new SqlCommand(sql1, MyConnection);
            SqlDataAdapter SelectAdapter = new SqlDataAdapter();
            SelectAdapter.SelectCommand = MyCommand;
            DataSet ds = new DataSet();
            MyConnection.Open();
            SelectAdapter.SelectCommand.ExecuteNonQuery();
            MyConnection.Close();
            SelectAdapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void 浏览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SqlConnection MyConnection = new SqlConnection(MyConn);
            SqlCommand MyCommand = new SqlCommand("SELECT * FROM view_emp", MyConnection);
            SqlDataAdapter SelectAdapter = new SqlDataAdapter();
            SelectAdapter.SelectCommand = MyCommand;
            DataSet ds = new DataSet();
            MyConnection.Open();
            SelectAdapter.SelectCommand.ExecuteNonQuery();
            MyConnection.Close();
            SelectAdapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection MyConnection = new SqlConnection(MyConn);
            string sql1 = "SELECT * FROM view_emp where 部门 = '" + comboBox2.Text + "'";
            SqlCommand MyCommand = new SqlCommand(sql1, MyConnection);
            SqlDataAdapter SelectAdapter = new SqlDataAdapter();
            SelectAdapter.SelectCommand = MyCommand;
            DataSet ds = new DataSet();
            MyConnection.Open();
            SelectAdapter.SelectCommand.ExecuteNonQuery();
            MyConnection.Close();
            SelectAdapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)//薪资大于等于
        {
            try
            {
                SqlConnection MyConnection = new SqlConnection(MyConn);
                string sql1 = "SELECT * FROM view_emp where 薪资 >= " + Convert.ToInt32(textBox2.Text);
                SqlCommand MyCommand = new SqlCommand(sql1, MyConnection);
                SqlDataAdapter SelectAdapter = new SqlDataAdapter();
                SelectAdapter.SelectCommand = MyCommand;
                DataSet ds = new DataSet();
                MyConnection.Open();
                SelectAdapter.SelectCommand.ExecuteNonQuery();
                MyConnection.Close();
                SelectAdapter.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("人力资源管理  " + ex.Message);
                textBox2.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)//薪资小于等于
        {
            try
            {
                SqlConnection MyConnection = new SqlConnection(MyConn);
                string sql1 = "SELECT * FROM view_emp where 薪资 <= " + Convert.ToInt32(textBox3.Text);
                SqlCommand MyCommand = new SqlCommand(sql1, MyConnection);
                SqlDataAdapter SelectAdapter = new SqlDataAdapter();
                SelectAdapter.SelectCommand = MyCommand;
                DataSet ds = new DataSet();
                MyConnection.Open();
                SelectAdapter.SelectCommand.ExecuteNonQuery();
                MyConnection.Close();
                SelectAdapter.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch(Exception ex)
            {
                MessageBox.Show("人力资源管理  "+ex.Message);
                textBox3.Text = "";
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(dataGridView1.CurrentRow.Cells[0].Value.ToString() == null || dataGridView1.CurrentRow.Cells[0].Value.ToString()=="")
            {
                MessageBox.Show("请选择要删除的行!","人力资源管理系统");
                return;
            }
            DialogResult drt = MessageBox.Show("确定删除第"+ dataGridView1.CurrentRow.Cells[0].Value.ToString()+"行吗?", "人力资源管理系统",MessageBoxButtons.YesNo);
            if(drt!=DialogResult.Yes)
            {
                return;
            }
            try
            {
                SqlConnection MyConnection = new SqlConnection(MyConn);
                SqlCommand MyCommand = new SqlCommand("delete from emp where id="+ dataGridView1.CurrentRow.Cells[0].Value, MyConnection);
                SqlDataAdapter SelectAdapter = new SqlDataAdapter();
                SelectAdapter.SelectCommand = MyCommand;
                MyConnection.Open();
                SelectAdapter.SelectCommand.ExecuteNonQuery();

                MyConnection.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 更新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell.RowIndex < 0) { return; }
            Form2 f2 = new mpmng.Form2();

            f2.button1Text = "更新";
            f2.captionText = "更新人员信息";
            f2.objectText = Int32.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            f2.textBox1Text= dataGridView1.CurrentRow.Cells[1].Value.ToString();
            f2.numericUpDownvalue1 = Convert.ToDecimal(dataGridView1.CurrentRow.Cells[2].Value.ToString());
            f2.textBox4Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            f2.textBox5Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            f2.textBox6Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            f2.combo1Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            f2.numericUpDownvalue2 = Convert.ToDecimal(dataGridView1.CurrentRow.Cells[8].Value.ToString());
            if(dataGridView1.CurrentRow.Cells[3].Value.ToString()=="男")
            {
                f2.radiobuttonvalue1 = true; f2.radiobuttonvalue2 = false;
            }
            else
            {
                f2.radiobuttonvalue1 = false; f2.radiobuttonvalue2 = true;
            }
            f2.ShowDialog();
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace mpmng
{
    public partial class Form2 : Form
    {
        string MyConn = "server=localhost;uid=sa;pwd=xx123xx;database=dxp;";
        SqlConnection MyConnection = null;
        DataSet ds1 = new DataSet();
        private int objectid = 0;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("群众");
            comboBox1.Items.Add("党员");
            comboBox1.Items.Add("团员");
            comboBox1.Text = "党员";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region 添加
            if (button1.Text == "添加")
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("人力资源管理,姓名不能为空!");
                }
                string name = textBox1.Text;
                int age = (int)numericUpDown1.Value;
                string sex = "男";
                if (radioButton1.Checked == true)
                {
                    sex = "男";
                }
                else
                {
                    sex = "女";
                }
                string dept = textBox4.Text;
                string position = textBox5.Text;
                string prof = textBox6.Text;
                string poliface = comboBox1.Text;
                int salary = (int)numericUpDown2.Value;

                string sql1 = "insert into emp values ('" + name + "'," + age + ",'" + sex + "','" + dept + "','" + position + "','" + prof + "','" + poliface + "'," + salary + ")";

                try
                {
                    SqlConnection MyConnection = new SqlConnection(MyConn);
                    SqlCommand MyCommand = new SqlCommand(sql1, MyConnection);
                    SqlDataAdapter SelectAdapter = new SqlDataAdapter();
                    SelectAdapter.SelectCommand = MyCommand;
                    DataSet ds = new DataSet();
                    MyConnection.Open();
                    SelectAdapter.SelectCommand.ExecuteNonQuery();
                    MyConnection.Close();
                    this.Hide();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("人力资源管理,添加," + ex.Message);
                }
            }
            #endregion
            #region 更新
            if (button1.Text == "更新")
            {
                string sexstr = "男";
                if (radioButton1.Checked == true) { sexstr = "男"; }
                if (radioButton2.Checked == true) { sexstr = "女"; }

                string sql2 = "UPDATE emp SET name='"+textBox1.Text+"',age="+Convert.ToInt32(numericUpDown1.Value)+", sex='"+sexstr+"',dept='"+textBox4.Text+"',position='"+textBox5.Text+"',prof='"+textBox6.Text+"',poliface='"+comboBox1.Text+"',salary="+Convert.ToInt32(numericUpDown2.Value)+" WHERE id="+objectid;

                try
                {
                    SqlConnection MyConnection = new SqlConnection(MyConn);
                    SqlCommand MyCommand = new SqlCommand(sql2, MyConnection);
                    SqlDataAdapter SelectAdapter = new SqlDataAdapter();
                    SelectAdapter.SelectCommand = MyCommand;
                    
                    MyConnection.Open();
                    SelectAdapter.SelectCommand.ExecuteNonQuery();
                    MyConnection.Close();
                    this.Hide();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("人力资源管理,更新," + ex.Message);
                }
            }
            #endregion

            }

        #region 传值变量
        public int objectText
        {
            get { return this.objectid; }
            set { this.objectid = value; }
        }

        public string button1Text
        {
            get { return this.button1.Text; }
            set { this.button1.Text = value; }
        }

        public string captionText
        {
            get { return this.Text; }
            set { this.Text = value; }
        }

        public string textBox1Text
        {
            get { return this.textBox1.Text; }
            set { this.textBox1.Text = value; }
        }

        public string textBox4Text
        {
            get { return this.textBox4.Text; }
            set { this.textBox4.Text = value; }
        }

        public string textBox5Text
        {
            get { return this.textBox5.Text; }
            set { this.textBox5.Text = value; }
        }

        public string textBox6Text
        {
            get { return this.textBox6.Text; }
            set { this.textBox6.Text = value; }
        }

        public decimal numericUpDownvalue1
        {
            get { return this.numericUpDown1.Value; }
            set { this.numericUpDown1.Value = value; }
        }

        public bool radiobuttonvalue1
        {
            get { return this.radioButton1.Checked; }
            set { this.radioButton1.Checked = value; }
        }

        public bool radiobuttonvalue2
        {
            get { return this.radioButton2.Checked; }
            set { this.radioButton2.Checked = value; }
        }

        public string combo1Text
        {
            get { return this.comboBox1.Text; }
            set { this.comboBox1.Text = value; }
        }

        public decimal numericUpDownvalue2
        {
            get { return this.numericUpDown2.Value; }
            set { this.numericUpDown2.Value = value; }
        }

        #endregion
    }
}

只要把表的字段更改;代码中相应字段处更改;就能变成别的单表增删改查程序;拿去花吧;

有活可以联系我;

发布了434 篇原创文章 · 获赞 512 · 访问量 294万+

猜你喜欢

转载自blog.csdn.net/bcbobo21cn/article/details/103739133
今日推荐