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 MySchoolDataSet
{
public partial class Form1 : Form
{
DBHelper d = new DBHelper();
DataSet ds = new DataSet();
SqlDataAdapter adap = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
///控制台显示的程序!!!!
getSHow();
SearchStudent();
}
public void getSHow()此方法显示年级的所有信息!!!
{
// DBHelper d = new DBHelper();
string sql = "SELECT * FROM GRADE ";
// DataSet ds = new DataSet();//创建数据集对象
//创建DataAdapter
adap = new SqlDataAdapter(sql, d.Connection);
try
{
//填充数据集
adap.Fill(ds, "grade");
comboBox1.DataSource=ds.Tables["grade"];
comboBox1.ValueMember = "gradeId";
comboBox1.DisplayMember = "gradename";//实际值
//向数据集年级表的第一行添加数据"全部"
DataRow row = ds.Tables["grade"].NewRow();
row["gradeid"] = -1;
row["gradeName"] = "全部";
ds.Tables["grade"].Rows.InsertAt(row, 0);
//打印数据局中的GRADE表
//foreach (DataRow row in ds.Tables["grade"].Rows)
//{
// Console.WriteLine("{0}\t{1}", row["gradeid"], row["gradeName"]);
//}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();//退出按钮+方法!!!
}
private void button1_Click(object sender, EventArgs e)
{
string shiji = this.comboBox1.SelectedValue.ToString();
string neirong = this.comboBox1.Text;
MessageBox.Show("你选中的内容是:" + neirong + "实际值是:" + shiji, "提示");
}
public void SearchStudent()
{
//DBHelper d = new DBHelper();
// string sql = @"SELECT [StudentNo],[StudentName] ,[Sex] ,[BornDate]
// FROM [MySchool].[dbo].[Student]";
string sql = "SELECT * FROM student ";
// if (Convert.ToInt32(this.comboBox1.SelectedValue)!=-1)
if(! this.comboBox1.Text.Equals("全部"))
{
sql += " where gradeid=" + this.comboBox1.SelectedValue;
}
// DataSet ds = new System.Data.DataSet();//创建数据集对象
//创建DataAdapter
adap = new SqlDataAdapter(sql, d.Connection);
try
{
//填充钱,先清空原有的数据!!!
if (ds.Tables["stu"] != null)
{
ds.Tables["stu"].Clear();
}
adap.Fill(ds,"stu");
this.dataGridView1.AutoGenerateColumns = false;//此方法去掉多余自动生成的所有列!!!!!!
dataGridView1.DataSource = ds.Tables["stu"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)
{
//if (ds.Tables["stu"] != null)
//{
// ds.Tables["stu"].Clear();
//}
SearchStudent();
}
private void 获取IDToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(this.dataGridView1.SelectedCells[0].Value.ToString());
}
private void button4_Click(object sender, EventArgs e)
{
DialogResult i=MessageBox.Show("确定要保存吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (i==DialogResult.OK)
{
SqlCommandBuilder builder = new SqlCommandBuilder(adap);
MessageBox.Show("保存成功!!!");
adap.Update(ds, "stu");
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
}
}
使用C#数据库应用系统 第五章上机练习
最新推荐文章于 2018-08-09 15:55:00 发布
这篇博客展示了如何使用C#进行数据库操作,通过`DBHelper`类连接数据库并实现数据显示。在`Form1`中,`getSHow()`方法用于加载年级所有信息到下拉框,并在用户选择后,`SearchStudent()`方法根据所选年级查询学生信息并显示在数据网格视图中。此外,还包括了数据的筛选、保存和获取ID等功能。
摘要由CSDN通过智能技术生成