📄 student.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace kaoqin.stu
{
public partial class student : Form
{
public student()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
{
Add();
}
else if(this.radioButton2.Checked == true)
{
Del();
}
else if(this.radioButton3.Checked == true)
{
Mod();
}
else
{
Query();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
{
this.setDefault();
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton2.Checked == true)
{
this.groupBox4.Enabled = false;
this.BindGrid();
}
else
{
this.groupBox4.Enabled = true;
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true)
{
BindGrid();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void student_Load(object sender, EventArgs e)
{
BindData();
}
private void button3_Click(object sender, EventArgs e)
{
this.Query();
}
private void BindData()
{
this.comboBox1.Items.Clear();
string sql = "select * from depart";
DataTable dt = DB.getTable(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
this.comboBox1.Items.Add(dt.Rows[i]["departName"].ToString().Trim());
}
sql = "select * from class ";
dt.Clear();
dt = DB.getTable(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
this.comboBox2.Items.Add(dt.Rows[i]["className"].ToString());
}
}
private void Add()
{
if (this.comboBox1.SelectedIndex == -1)
{
MessageBox.Show("请选择系部!");
this.comboBox1.Focus();
return;
}
if (this.comboBox2.SelectedIndex == -1)
{
MessageBox.Show("请选择班级!");
this.comboBox2.Focus();
return;
}
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("请填写学生姓名!");
this.textBox1.Focus();
return;
}
if (this.textBox2.Text.Trim() == "")
{
MessageBox.Show("请填写学生学号!");
this.textBox2.Focus();
return;
}
if (this.radioButton5.Checked != true && this.radioButton6.Checked != true)
{
MessageBox.Show("请选择性别!");
this.radioButton5.Focus();
return;
}
string sql = "select * from vwStudent where xuehao='"+this.textBox2.Text.Trim()
+"' and name='"+this.textBox1.Text.Trim()+"' and className='"+this.comboBox2.SelectedItem.ToString()
+"' and departName='"+this.comboBox1.SelectedItem.ToString()+"'";
DataTable dt = DB.getTable(sql);
if (dt.Rows.Count != 0)
{
this.dataGrid1.DataSource = dt;
MessageBox.Show("学生" + this.textBox1.Text.Trim() +"已存在!");
return;
}
sql = "select classID from class where className='" + this.comboBox2.SelectedItem.ToString() + "'";
string claID = DB.getID(sql);
string xh = this.textBox2.Text.Trim();
string name = this.textBox1.Text.Trim();
string sex = "男";
if (this.radioButton6.Checked == true)
{
sex = "女";
}
sql = "insert into student values('"+xh+"','"+name+"','"+sex+"','"+claID+"')";
if (DB.executeSql(sql))
{
MessageBox.Show("操作成功!");
sql = "select * from vwStudent where className='" + this.comboBox2.SelectedItem.ToString()
+ "' and departName='" + this.comboBox1.SelectedItem.ToString() + "'";
dt.Clear();
dt = DB.getTable(sql);
this.dataGrid1.DataSource = dt;
this.setDefault();
}
else
{
MessageBox.Show("操作失败!");
}
}
private void Mod()
{
if (this.lblID.Text == "")
{
MessageBox.Show("请选择一个要修改的学生!");
return;
}
if (this.comboBox1.SelectedIndex == -1)
{
MessageBox.Show("请选择系部!");
this.comboBox1.Focus();
return;
}
if (this.comboBox2.SelectedIndex == -1)
{
MessageBox.Show("请选择班级!");
this.comboBox2.Focus();
return;
}
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("请填写学生姓名!");
this.textBox1.Focus();
return;
}
if (this.textBox2.Text.Trim() == "")
{
MessageBox.Show("请填写学生学号!");
this.textBox2.Focus();
return;
}
if (this.radioButton5.Checked != true && this.radioButton6.Checked != true)
{
MessageBox.Show("请选择性别!");
this.radioButton5.Focus();
return;
}
string s="男";
if(this.radioButton6.Checked==true)
{
s="女";
}
string sql = "select * from vwStudent where xuehao='" + this.textBox2.Text.Trim()
+ "' and name='" + this.textBox1.Text.Trim() + "' and className='" + this.comboBox2.SelectedItem.ToString()
+ "' and departName='" + this.comboBox1.SelectedItem.ToString() + "' and sex='"+s+"'";
DataTable dt = DB.getTable(sql);
if (dt.Rows.Count != 0)
{
this.dataGrid1.DataSource = dt;
MessageBox.Show("学生" + this.textBox1.Text.Trim() + "已存在!");
return;
}
sql = "select classID from class where className='" + this.comboBox2.SelectedItem.ToString() + "'";
string claID = DB.getID(sql);
string xh = this.textBox2.Text.Trim();
string name = this.textBox1.Text.Trim();
string sex = "男";
if (this.radioButton6.Checked == true)
{
sex = "女";
}
sql = "update student set xuehao='" + xh + "', name='" + name + "', sex='" + sex + "',classID='" + claID + "' where studentID='" + this.lblID.Text+"'";
if (DB.executeSql(sql))
{
MessageBox.Show("操作成功!");
sql = "select * from vwStudent where className='" + this.comboBox2.SelectedItem.ToString()
+ "' and departName='" + this.comboBox1.SelectedItem.ToString() + "'";
dt.Clear();
dt = DB.getTable(sql);
this.dataGrid1.DataSource = dt;
}
else
{
MessageBox.Show("操作失败!");
}
}
private void Del()
{
if (this.lblID.Text == "")
{
MessageBox.Show("请选择一个要修改的学生!");
return;
}
string sql = "delete student where studentID='"+this.lblID.Text+"' ";
if (MessageBox.Show("你真的想要删除此记录吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
{
return;
}
if (DB.executeSql(sql))
{
MessageBox.Show("操作成功!");
this.setDefault();
sql = "select * from vwStudent where className='" + this.comboBox2.SelectedItem.ToString()
+ "' and departName='" + this.comboBox1.SelectedItem.ToString() + "'";
DataTable dt = DB.getTable(sql);
this.dataGrid1.DataSource = dt;
this.BindGrid();
}
else
{
MessageBox.Show("操作失败!");
}
}
private void Query()
{
if (this.comboBox1.SelectedIndex == -1 && this.comboBox2.SelectedIndex==-1 && this.textBox1.Text.Trim()=="" &&this.textBox2.Text.Trim()=="")
{
MessageBox.Show("至少选择一个查询条件!");
this.comboBox2.Focus();
return;
}
string depart="";
string classes="";
if (this.comboBox1.SelectedIndex != -1)
{
depart = this.comboBox1.SelectedItem.ToString();
}
if (this.comboBox2.SelectedIndex != -1)
{
classes = this.comboBox2.SelectedItem.ToString();
}
string stuName = this.textBox1.Text.Trim();
string stuID = this.textBox2.Text.Trim();
bool where = false;
string sql = "select * from vwStudent ";
if (depart.Trim() != "")
{
sql += "where departName='"+depart+"'";
where = true;
}
if (classes.Trim() != "")
{
if (where)
{
sql += " and className='" + classes + "' ";
}
else
{
sql += " where className='" + classes + "' ";
where = true;
}
}
if (stuName.Trim() != "")
{
if (where)
{
sql += " and name='" + stuName + "' ";
}
else
{
sql += " where name='" + stuName + "' ";
where = true;
}
}
if (stuID.Trim() != "")
{
if (where)
{
sql += " and xuehao='" + stuID + "' ";
}
else
{
sql += " where xuehao='" + stuID + "' ";
where = true;
}
}
DataTable dt = DB.getTable(sql);
if (dt.Rows.Count == 0)
{
this.dataGrid1.DataSource = null;
MessageBox.Show("对不起,找不到此信息!");
this.lblID.Text = "";
return;
}
this.dataGrid1.DataSource = dt;
}
private void setDefault()
{
this.lblID.Text = "";
this.textBox2.Text = "";
this.textBox1.Text = "";
}
private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
{
}
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
if (this.radioButton3.Checked != true && this.radioButton2.Checked != true)
{
return;
}
if (this.dataGrid1.CurrentRowIndex == -1)
{
return;
}
this.comboBox1.SelectedItem = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, 6].ToString();
this.comboBox2.SelectedItem = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, 5].ToString();
this.textBox1.Text = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, 2].ToString();
this.textBox2.Text = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, 1].ToString();
if (this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, 3].ToString().Trim() == "男")
{
this.radioButton5.Checked = true;
}
else
{
this.radioButton6.Checked = true;
}
this.lblID.Text = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, 0].ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -