日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2025/4/14 C# 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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} }

只要把表的字段更改;代碼中相應字段處更改;就能變成別的單表增刪改查程序;拿去花吧;

有活可以聯系我;

?

總結

以上是生活随笔為你收集整理的C# 功能完整的单表增删改查程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。