用C#连接到数据库实现学生学籍管理系统(一)https://developer.aliyun.com/article/1382520
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.OleDb; namespace 学生学籍管理系统 { public partial class AddClass : Form { public AddClass() { InitializeComponent(); } private void button3_Click(object sender, EventArgs e) { textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = textBox5.Text = textBox6.Text = ""; } private void button2_Click(object sender, EventArgs e) { DB.cn.Close(); this.Close(); } private void button1_Click(object sender, EventArgs e) { string oldconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxjdb.mdb"; OleDbConnection oleDbConnection1 = new OleDbConnection(oldconn); oleDbConnection1.Close(); oleDbConnection1.Open(); string sql; OleDbCommand cmd = new OleDbCommand("", oleDbConnection1); if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "" || textBox5.Text.Trim() == "" || textBox6.Text.Trim() == "") { MessageBox.Show("请填写全部信息!", "提示"); } else { DB.cn.Close(); DB.cn.Open(); OleDbCommand oldcomm = new OleDbCommand("", DB.cn); sql = "select ClassNum from ClassInfo where ClassNum='" + textBox1.Text.Trim() + "'"; oldcomm.CommandText = sql; if (oldcomm.ExecuteScalar() != null) { MessageBox.Show("你输入的班级编号已经存在,请重新输入!", "错误提示"); } else { sql = "insert into ClassInfo values ('" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() +"' )"; oldcomm.CommandText = sql; oldcomm.ExecuteNonQuery(); MessageBox.Show("添加成功!", "提示"); DB.cn.Close(); } oleDbConnection1.Close(); } } private void AddClass_Load(object sender, EventArgs e) { DataSet ds = new DataSet(); OleDbDataAdapter adp = new OleDbDataAdapter("", DB.cn); adp.SelectCommand.CommandText = "select ClassName from ClassInfo"; adp.Fill(ds); textBox1.Focus(); } } }
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.OleDb; namespace 学生学籍管理系统 { public partial class CourseView : Form { public CourseView() { InitializeComponent(); } private void courseInfoBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.courseInfoBindingSource.EndEdit(); this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo); } private void CourseView_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“xsxjdbDataSet.courseInfo”中。您可以根据需要移动或移除它。 //this.courseInfoTableAdapter.Fill(this.xsxjdbDataSet.courseInfo); // TODO: 这行代码将数据加载到表“xsxjdbDataSet.courseInfo”中。您可以根据需要移动或移除它。 this.courseInfoTableAdapter.Fill(this.xsxjdbDataSet.courseInfo); } private void button1_Click(object sender, EventArgs e) { DataView dv = xsxjdbDataSet.courseInfo.DefaultView; dv.Sort = "SpeNum"; dv.RowFilter = "SpeNum='" + textBox1.Text.Trim() + "'"; dv.RowStateFilter = DataViewRowState.CurrentRows; courseInfoDataGridView.DataSource = dv; } private void button2_Click(object sender, EventArgs e) { DataView dv = xsxjdbDataSet.courseInfo.DefaultView; dv.Sort = ""; dv.RowFilter = ""; dv.RowStateFilter = DataViewRowState.CurrentRows; courseInfoDataGridView.DataSource = dv; } private void courseInfoBindingNavigatorSaveItem_Click_1(object sender, EventArgs e) { this.Validate(); this.courseInfoBindingSource.EndEdit(); this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo); } } }
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.OleDb; namespace 学生学籍管理系统 { public partial class Deletecourse : Form { public Deletecourse() { InitializeComponent(); } private void courseInfoBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.courseInfoBindingSource.EndEdit(); this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo); } private void Dealcourse_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“xsxjdbDataSet.courseInfo”中。您可以根据需要移动或移除它。 this.courseInfoTableAdapter.Fill(this.xsxjdbDataSet.courseInfo); } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void button1_Click(object sender, EventArgs e) { DB.cn.Close(); DB.cn.Open(); string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxjdb.mdb"; OleDbConnection sqlconn = new OleDbConnection (connstring); sqlconn.Open(); string sqlstring = "delete from CourseInfo where CourseName='" + textBox1.Text.Trim() + "'"; OleDbCommand sqlcomm = new OleDbCommand (sqlstring, sqlconn); sqlcomm.ExecuteNonQuery(); MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); textBox1.Clear(); textBox1.Focus(); } private void button3_Click(object sender, EventArgs e) { if (xsxjdbDataSet.HasChanges()) { DialogResult r = MessageBox.Show("真的修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (r == DialogResult.Yes) { this.Validate(); this.courseInfoBindingSource.EndEdit(); this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo); } } } } }
用C#连接到数据库实现学生学籍管理系统(三)https://developer.aliyun.com/article/1382524