C#常用增删改查源码.docx

上传人:小飞机 文档编号:3153769 上传时间:2023-03-11 格式:DOCX 页数:15 大小:39.17KB
返回 下载 相关 举报
C#常用增删改查源码.docx_第1页
第1页 / 共15页
C#常用增删改查源码.docx_第2页
第2页 / 共15页
C#常用增删改查源码.docx_第3页
第3页 / 共15页
C#常用增删改查源码.docx_第4页
第4页 / 共15页
C#常用增删改查源码.docx_第5页
第5页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C#常用增删改查源码.docx》由会员分享,可在线阅读,更多相关《C#常用增删改查源码.docx(15页珍藏版)》请在三一办公上搜索。

1、C#常用增删改查源码这个是添加的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace 学生成绩管理系统 public partial class studentluru : Form / CurrencyManager cmAmend; /

2、 SqlConnection sqlConnection1 = new SqlConnection; public studentluru InitializeComponent; private void studentluru_Load(object sender, EventArgs e) /录入 private void button5_Click(object sender, EventArgs e) string sno = textBox1.Text; string sname = textBox2.Text; string ssex = textBox3.Text; strin

3、g sage = textBox4.Text; string sclass = textBox5.Text; string sdept = textBox6.Text; string saddress = textBox7.Text; string sphone = textBox8.Text; string sqq = textBox9.Text; if (textBox1.Text = | textBox2.Text = | textBox5.Text = ) if (textBox1.Text = ) MessageBox.Show(学号不能为空, 警告提示, MessageBoxBut

4、tons.OKCancel, MessageBoxIcon.Error); textBox1.Focus; return; if (textBox2.Text = ) MessageBox.Show(姓名不能为空, 警告提示, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox2.Focus; return; if (textBox5.Text = ) MessageBox.Show(班级不能为空, 警告提示, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox5

5、.Focus; return; else string connstr = server=IT32;uid=sa;pwd=sa;database=sc; try SqlConnection conn = new SqlConnection(connstr); conn.Open; string sqlinsert = insert into studentinfo values( + sno + , + sname + , + ssex + , + sage + , + sclass + , + sdept + , + saddress + , + sphone + , + sqq + );

6、SqlCommand sc = new SqlCommand(sqlinsert, conn); sc.ExecuteNonQuery; textBox1.Text = ; textBox2.Text = ; textBox3.Text = ; textBox4.Text = ; textBox5.Text = ; textBox6.Text = ; textBox7.Text = ; textBox8.Text = ; textBox9.Text = ; MessageBox.Show(数据已经添加成功, 温馨提示); conn.Close; catch (Exception ex) Mes

7、sageBox.Show(ex.ToString); private void button8_Click(object sender, EventArgs e) this.Dispose; 这个是查询界面的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient

8、; namespace 学生成绩管理系统 public partial class studentchaxun : Form DataTable dt = new DataTable; string per; string sno; CurrencyManager cmAmend; public studentchaxun InitializeComponent; public studentchaxun(string k, string s) InitializeComponent; per = k; sno = s; private void studentchaxun_Load(obje

9、ct sender, EventArgs e) / TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。 /this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo); /数据绑定 if (per = 超级用户) string sqlsel = select * from studentinfo ; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContextdt;

10、 this.dataGridView1.DataSource = dt; boBox1.DataSource = dt; boBox1.DisplayMember = sno; boBox2.DataSource = dt; boBox2.DisplayMember = sname; if (per = 普通用户) string sqlsel = select * from studentinfo where sno= + sno + ; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContextdt;

11、this.dataGridView1.DataSource = dt; boBox1.DataSource = dt; boBox1.DisplayMember = sno; boBox2.Enabled = false; this.radioButton2.Enabled = false; /this.toolStripButton1.Enabled = false; /this.toolStripButton2.Enabled = false; /this.toolStripButton3.Enabled = false; /this.toolStripButton4.Enabled =

12、false; /boBox2.DataSource = dt; /boBox2.DisplayMember = sname; DataTable bangding(string sqlsel) string connstr = server=.;uid=sa;pwd=sa;database=sc; using (SqlConnection conn = new SqlConnection(connstr) conn.Open; DataSet ds = new DataSet; SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); da.F

13、ill(ds, coust); DataTable dt = ds.Tablescoust; conn.Close; return dt; private void button1_Click(object sender, EventArgs e) if (this.radioButton1.Checked = true) string xuehao = comboBox1.Text; string sqlsel = select * from studentinfo where sno= + xuehao + ; this.dataGridView1.DataSource = bangdin

14、g(sqlsel); else string xingming = comboBox2.Text; string sqlsel = select * from studentinfo where sname= + xingming + ; this.dataGridView1.DataSource = bangding(sqlsel); private void radioButton1_CheckedChanged(object sender, EventArgs e) if (this.radioButton1.Checked = true) boBox1.Enabled = true;

15、boBox2.Enabled = false; else boBox1.Enabled = false; boBox2.Enabled = true; private void checkstate(int pos) if (pos = 0) toolStripButton1.Enabled = false; toolStripButton2.Enabled = false; toolStripButton3.Enabled = true; toolStripButton4.Enabled = true; else toolStripButton1.Enabled = true; toolSt

16、ripButton2.Enabled = true; toolStripButton3.Enabled = true; toolStripButton4.Enabled = true; /首记录 private void toolStripButton1_Click(object sender, EventArgs e) cmAmend.Position = 0;/设置为0.回到首记录 this.dataGridView1.Select; checkstate(cmAmend.Position); /上一条 private void toolStripButton2_Click(object

17、sender, EventArgs e) cmAmend.Position-; this.dataGridView1.Select; checkstate(cmAmend.Position); /下一条 private void toolStripButton3_Click(object sender, EventArgs e) cmAmend.Position+; this.dataGridView1.Select; checkstate(cmAmend.Position); /末记录 private void toolStripButton4_Click(object sender, Ev

18、entArgs e) cmAmend.Position = cmAmend.Count - 1; this.dataGridView1.Select; checkstate(cmAmend.Position); 删除的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlC

19、lient; namespace 学生成绩管理系统 public partial class studentshanchu : Form CurrencyManager cmAmend; public studentshanchu InitializeComponent; private void studentshanchu_Load(object sender, EventArgs e) / TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。 /this.studentinfoTableAdapter.Fill(thi

20、s.studentDataSet.studentinfo); /数据绑定 string sqlsel = select * from studentinfo ; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContextdt; this.dataGridView1.DataSource = dt; this.textBox1.DataBindings.Add(text, dt, sno); this.textBox2.DataBindings.Add(text, dt, sname); this.text

21、Box3.DataBindings.Add(text, dt, ssex); this.textBox4.DataBindings.Add(text, dt, sage); this.textBox5.DataBindings.Add(text, dt, sclass); this.textBox6.DataBindings.Add(text, dt, sdept); this.textBox7.DataBindings.Add(text, dt, saddress); this.textBox8.DataBindings.Add(text, dt, sphone); this.textBox

22、9.DataBindings.Add(text, dt, sqq); DataTable bangding(string sqlsel) string connstr = server=.;uid=sa;pwd=sa;database=sc; using (SqlConnection conn = new SqlConnection(connstr) conn.Open; DataSet ds = new DataSet; SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); da.Fill(ds, coust); DataTable dt

23、 = ds.Tablescoust; conn.Close; return dt; private void checkBox1_CheckedChanged(object sender, EventArgs e) if (checkBox1.Checked = true) this.Height = 450; else this.Height = 250; private void button1_Click(object sender, EventArgs e) if (MessageBox.Show(你确定要删除该记录吗, 询问, MessageBoxButtons.OKCancel,

24、MessageBoxIcon.Question) = DialogResult.OK) int pos = this.dataGridView1.CurrentCell.RowIndex; /获取该行 string connstr = server=.;uid=sa;pwd=sa;database=sc; using (SqlConnection conn = new SqlConnection(connstr) conn.Open; DataSet ds = new DataSet; string sqlset = select * from studentinfo; /数据集 SqlDat

25、aAdapter da = new SqlDataAdapter(sqlset, conn); da.Fill(ds, coust); DataTable dt = ds.Tablescoust; SqlCommandBuilder cb = new SqlCommandBuilder(da); dt.Rowspos.Delete; da.Update(ds, coust); textBox1.Text = ; textBox2.Text = ; textBox3.Text = ; textBox4.Text = ; textBox5.Text = ; textBox6.Text = ; te

26、xtBox7.Text = ; textBox8.Text = ; textBox9.Text = ; this.dataGridView1.DataSource = bangding(sqlset); MessageBox.Show(恭喜你已成功删除,温馨提示); conn.Close; 修改的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using Syst

27、em.Windows.Forms; using System.Data.SqlClient; namespace 学生成绩管理系统 public partial class studentxiugai : Form string connstr = server=.;uid=sa;pwd=sa;database=sc; string per; string sno; CurrencyManager cmAmend; public studentxiugai InitializeComponent; public studentxiugai(string k,string s) Initiali

28、zeComponent; per = k; sno = s; private void checkBox1_CheckedChanged(object sender, EventArgs e) if (checkBox1.Checked = true) /this.Height = 450; comboBox1.Enabled = false; textBox1.Enabled = false; else /this.Height = 250; comboBox1.Enabled = true; textBox1.Enabled = true; private void studentxiug

29、ai_Load(object sender, EventArgs e) /数据绑定 if (per = 超级用户) string sqlsel = select * from studentinfo order by sno asc ; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContextdt; boBox1.DataSource = dt; boBox1.DisplayMember = sno; this.textBox1.DataBindings.Add(text, dt, sname); th

30、is.textBox2.DataBindings.Add(text, dt, ssex); this.textBox3.DataBindings.Add(text, dt, sage); this.textBox4.DataBindings.Add(text, dt, sclass); this.textBox5.DataBindings.Add(text, dt, sdept); this.textBox6.DataBindings.Add(text, dt, saddress); this.textBox7.DataBindings.Add(text, dt, sphone); this.

31、textBox8.DataBindings.Add(text, dt, sqq); if (per = 普通用户) string sqlsel = select *from studentinfo where sno= + sno + ; DataTable dt = bangding(sqlsel); boBox1.DataSource = dt; boBox1.DisplayMember = sno; this.textBox1.DataBindings.Add(text, dt, sname); this.textBox2.DataBindings.Add(text, dt, ssex)

32、; this.textBox3.DataBindings.Add(text, dt, sage); this.textBox4.DataBindings.Add(text, dt, sclass); this.textBox5.DataBindings.Add(text, dt, sdept); this.textBox6.DataBindings.Add(text, dt, saddress); this.textBox7.DataBindings.Add(text, dt, sphone); this.textBox8.DataBindings.Add(text, dt, sqq); Da

33、taTable bangding(string sqlsel) / string connstr = server=.;uid=sa;pwd=sa;database=sc; using (SqlConnection conn = new SqlConnection(connstr) conn.Open; DataSet ds = new DataSet; SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); da.Fill(ds, coust); DataTable dt = ds.Tablescoust; conn.Close; retu

34、rn dt; private void button1_Click(object sender, EventArgs e) if (checkBox1.Checked = true) string sno = comboBox1.Text; string sname = textBox1.Text; string sex = textBox2.Text; string age = textBox3.Text; string sclass = textBox4.Text; string sdept = textBox5.Text; string saddress = textBox6.Text;

35、 string sphone = textBox7.Text; string sqq = textBox8.Text; if (textBox2.Text = | textBox3.Text = | textBox4.Text = | textBox5.Text = ) if (textBox2.Text = ) MessageBox.Show(性别不能为空, 温馨提示, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox2.Focus; return; if (textBox3.Text = ) MessageBox.Show

36、(年龄不能为空, 温馨提示, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox3.Focus; return; if (textBox4.Text = ) MessageBox.Show(班级不能为空, 温馨提示, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox4.Focus; return; if (textBox5.Text = ) MessageBox.Show(系别不能为空, 温馨提示, MessageBoxButtons.OKCancel, Mes

37、sageBoxIcon.Error); textBox5.Focus; return; else string connstr = server=IT32;uid=sa;pwd=sa;database=sc; try SqlConnection conn = new SqlConnection(connstr); conn.Open; string sqlupdate = update studentinfo set ssex= + sex + ,sage= + age + ,sclass= + sclass + ,sdept= + sdept + ,saddress= + saddress + ,sphone= + sphone + ,sqq= + sqq + where sno= + sno + ; SqlCommand sc = new SqlCommand(sqlupdate, conn); sc.ExecuteNonQuery; MessageBox.Show(数据已经修改成功, 温馨提示); conn.Close; catch (Exception ex) MessageBox.Show(ex.ToString); else MessageBox.Show(对不起,你还没有选中我要修改,所以您无法修改,温馨提示);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号