最后一本书 第五章上机练习1-4
2016-12-11 18:46
309 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace 最后一本书_第五章上机 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataSet ds = new DataSet(); public string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; public void getGrade() { MySchool.DBHelper db = new MySchool.DBHelper(); string sql = "select * from Grade"; SqlDataAdapter adapter = new SqlDataAdapter(sql,db.Connection); adapter.Fill(ds,"Grade"); //foreach(DataRow row in ds.Tables[0].Rows){ // Console.WriteLine("{0} \t {1}",row["GradeId"],row["GradeName"]); //} this.comboBox1.DataSource = ds.Tables["Grade"]; this.comboBox1.ValueMember = "GradeId"; this.comboBox1.DisplayMember = "GradeName"; DataRow row = ds.Tables["Grade"].NewRow(); row["GradeId"] = -1; row["GradeName"] = "全部"; ds.Tables["Grade"].Rows.InsertAt(row,0); } private void Form1_Load(object sender, EventArgs e) { getGrade(); SearchStudent(); } private void button1_Click(object sender, EventArgs e) { //getGrade(); SearchStudent(); //string s = this.comboBox1.SelectedValue.ToString(); //string d = this.comboBox1.Text; //MessageBox.Show("内容" + s + "实际" + d, "提示"); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } public void SearchStudent() { MySchool.DBHelper s = new MySchool.DBHelper(); try { string sql="select * from student"; if (!this.comboBox1.Text.Equals("全部")) { sql += " where GradeId=" + this.comboBox1.SelectedValue; } SqlDataAdapter adapter = new SqlDataAdapter(sql,s.Connection); if (ds.Tables["Student"] != null) { ds.Tables["Student"].Clear(); } adapter.Fill(ds,"Student"); } catch (Exception e) { MessageBox.Show(e.Message); } this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.DataSource = ds.Tables["Student"]; } private void 保存_Click(object sender, EventArgs e) { MessageBox.Show("确认要修改么?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); MySchool.DBHelper s = new MySchool.DBHelper(); string sql = "select * from student"; SqlDataAdapter adapter = new SqlDataAdapter(sql,s.Connection); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); adapter.Update(ds, "Student"); } private void 查询IdToolStripMenuItem_Click(object sender, EventArgs e) { } private void 添加学生成绩ToolStripMenuItem_Click(object sender, EventArgs e) { MySchool.DBHelper s = new MySchool.DBHelper(); MessageBox.Show(this.dataGridView1.SelectedCells[0].Value.ToString(),"提示"); } } }
相关文章推荐
- 最后一本书 第五章 课后练习2,3,4
- HTML第五章上机练习1
- 算法与数据结构第五章树上机练习
- HTML第五章上机练习5
- 最后一本书 上机5(翻书)
- 使用C#数据库应用系统 第五章上机练习
- HTML第五章上机练习5
- 第五章上机练习5畅销书排行榜
- 最后一本书 第六章课后练习3,4
- 面向对象程序设计上机练习一(函数重载)
- 面向对象程序设计上机练习八(对象数组)
- 十二章上机 练习4
- C++ PRIMER第五章课后练习
- 面向对象程序设计上机练习九(对象指针)
- 第二章上机练习
- 2012年下半年版无忧全国计算机等级考试三级PC技术上机练习第34套运行结果
- 《Java编程思想》第五章练习5答案
- 面向对象程序设计上机练习五(类和对象)
- 面向对象程序设计上机练习四(变量引用)
- 面向对象程序设计上机练习七(类和对象)