📄 form1.cs
字号:
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 ADO
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//把reader中数据显示在listBox1中
private void ListViewData(OleDbDataReader reader)
{
listBox1.Items.Clear();
while (reader.Read())
{
string row = "";
for (int i = 0; i < reader.FieldCount; i++)
row += reader[i].ToString() + " ";
listBox1.Items.Add(row);
comboBoxUpdateID.Items.Add(reader[0].ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“教学管理DataSet.students”中。您可以根据需要移动或移除它。
btnViewAllData_Click(this, e);
comboBoxUpdateID.SelectedIndex = 0; //修改部分的I学号默认为第一个学生的学号
comboBoxField.SelectedIndex = 0; //查询部分,字段名为学号
}
//把table中数据显示在dataGridView中
private void GridViewData(DataTable table)
{
this.dataGridView1.DataSource = table;
}
private void btnViewAllData_Click(object sender, EventArgs e)
{
Students stu = new Students();//建立学生类Students对象stu
ListViewData(stu.GetReaderOfAllStudents());//调用学生类students的GetReaderOfAllStudents返回Reader,并显示在listBox1
GridViewData(stu.GetTableOfAllStudents());//调用学生类Students的GetTableOFAllStudents返回Table,并显示在dataViewData1
}
private void btnQuery_Click(object sender, EventArgs e)
{
string FieldName = comboBoxField.Text;
string value = textValue.Text;
Students stu = new Students();
ListViewData(stu.QueryReaderOfStudents(FieldName, value));
GridViewData(stu.QueryTableOfStudents(FieldName, value));
}
//修改学生信息
private void btnUpdate_Click(object sender, EventArgs e)
{
string sex = "";
if (radioButton1.Checked)
sex = "男";
else
sex = "女";
Students stu = new Students();
stu.UpdateStudent(comboBoxUpdateID.Text, textUpdateName.Text, sex, Convert.ToInt32(textUpdateAge.Text), dateUpdate.Value, textUpdateAdd.Text, textUpdatePro.Text, textUpdateGrade.Text, textUpdateClass.Text, textUpdateTel.Text, Convert.ToInt32(textUpdateGK.Text));
btnViewAllData_Click(sender, e);
}
private void btnInsert_Click(object sender, EventArgs e)
{
string sex = "";
if (textNewID.Text == "")
MessageBox.Show("请输入学号!","提示");
else if (textNewName.Text == "")
MessageBox.Show("请输入姓名!","提示");
else if (textNewAge.Text == "")
MessageBox.Show("请输入你的年龄!", "提示");
else if (Convert.ToInt32(textNewAge.Text) >= 35) //设置最在允许输入年龄为35
MessageBox.Show("请输入正确的年龄!", "提示");
else if (textNewAddress.Text == "")
MessageBox.Show("请输入籍贯!", "提示");
else if (textNewPro.Text == "")
MessageBox.Show("请输入专业!", "提示");
else if (toaction == false)
MessageBox.Show("请选择你的出生日期!", "提示");
else if (textNewGrade.Text == "")
MessageBox.Show("请输入你所在年级!", "提示");
else if (textNewClass.Text == "")
MessageBox.Show("请输入你所在班级!", "提示");
else
{
if (radioButton3.Checked)
sex = "男";
else
sex = "女";
Students stu = new Students();
stu.InsertStudent(textNewID.Text, textNewName.Text, sex, Convert.ToInt32(textNewAge.Text), dateTimeNew.Value, textNewAddress.Text, textNewPro.Text, textNewGrade.Text, textNewClass.Text, textNewTel.Text, Convert.ToInt32(textNewGK.Text));
btnViewAllData_Click(sender, e);
}
}
//listBox1选择不同的记录
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBoxUpdateID.SelectedIndex = listBox1.SelectedIndex;
this.dataGridView1.Rows[comboBoxUpdateID.SelectedIndex].Selected = true;// .CurrentRow.Index= comboBox1.SelectedIndex;
}
//在dataGridView1被单击时,切换listBox1和comboBoxUpdateID中相应当前记录
private void dataGridView1_Click(object sender, EventArgs e)
{
int i = this.dataGridView1.CurrentRow.Index;
if (i < listBox1.Items.Count)
{
listBox1.SelectedIndex = i; comboBoxUpdateID.SelectedIndex = i;
}
}
//修改部分的学号改变
private void comboBoxUpdateID_SelectedIndexChanged(object sender, EventArgs e)
{
string FieldName = comboBoxField.Text;
string value = textValue.Text;
Students stu = new Students();
OleDbDataReader reader = stu.QueryReaderOfStudents("学号", comboBoxUpdateID.Text);
if (reader.Read())
{
textUpdateName.Text = reader["姓名"].ToString();
if (reader["性别"].ToString() == "男")
radioButton1.Checked = true;
else
radioButton2.Checked = true;
textUpdateAge.Text = reader["年龄"].ToString();
dateUpdate.Value =Convert.ToDateTime(reader["出生日期"].ToString());
textUpdateAdd.Text = reader["籍贯"].ToString();
textUpdatePro.Text = reader["专业"].ToString();
textUpdateGrade.Text = reader["年级"].ToString();
textUpdateClass.Text = reader["班级"].ToString();
textUpdateTel.Text = reader["电话号码"].ToString();
textUpdateGK.Text = reader["高考分数"].ToString();
}
reader.Close();
//更新其它显示控件的当前记录
listBox1.SelectedIndex = comboBoxUpdateID.SelectedIndex;
this.dataGridView1.Rows[comboBoxUpdateID.SelectedIndex].Selected = true; // .CurrentRow.Index= comboBox1.SelectedIndex;
}
private bool toaction = false;
private void dateTimeNew_ValueChanged(object sender, EventArgs e)
{
toaction = true;
}
private void btnQuerys_Click(object sender, EventArgs e)
{
string value1 = textFindName.Text;
string value2 = comboBoxFindSex.Text;
string value3 = textFindAge.Text;
string value4 = textFindAdd.Text;
string value5 = textFindPro.Text;
string value6 = comboBoxFindGrade.Text;
string value7 = comboBoxFindClass.Text;
string value8 = textFindGK.Text;
Students stu = new Students();
ListViewData(stu.QueryReaderOfSelectStudents(value1, value2, value3, value4, value5, value6, value7, value8));
GridViewData(stu.QueryTableOfSelectStudents(value1, value2, value3, value4, value5, value6, value7, value8));
}
private void btnDeleteData_Click(object sender, EventArgs e)
{
DialogResult r = MessageBox.Show("是否删除当前记录!", "删除当前记录!", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (r == DialogResult.Yes)
{
try
{
Students stu = new Students();//建立学生类Students对象stu
//ListViewData(stu.GetReaderOfDeleteStudents(comboBoxUpdateID.Text));//调用学生类students的GetReaderOfAllStudents返回Reader,并显示在listBox1
GridViewData(stu.GetTableOfDeleteStudents(comboBoxUpdateID.Text));//调用学生类Students的GetTableOFAllStudents返回Table,并显示在dataViewData1
}
catch (Exception ed)
{
MessageBox.Show("删除记录错误信息: " + ed.ToString(), "错误!");
}
}
else
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -