📄 weihudepartmen.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class WeihuDepartmen : Form
{
SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
baseclass.BaseOperate boperate = new baseclass.BaseOperate();
baseclass.OperateAndValidate opAndvalidate = new baseclass.OperateAndValidate();
//获取院系表信息
protected string M_str_sql = "select department as 院系名称 from department ";
//获取专业表中的信息
protected string M_str_sql1 = "select department as 院系名称 , Profession as 专业 from Profession ";
//获取班级表中的信息
protected string M_str_sql2 = "select Profession as 专业 ,class as 班级 from class";
//院系表
protected string M_str_table = "department";
//专业表
protected string M_str_table1 = "Profession";
//班级表
protected string M_str_table2 = "class";
protected int M_int_judge;
public string str, sql;
public WeihuDepartmen()
{
InitializeComponent();
}
private void WeihuDepartmen_Load(object sender, EventArgs e)
{
DataSet myds = boperate.getds(M_str_sql, M_str_table);
dataGridView1.DataSource = myds.Tables[0];
if (myds.Tables[0].Rows.Count > 0)
btnDelete.Enabled = true;
else
btnDelete.Enabled = false;
}
#region 添加院系信息
/// <summary>
/// 添加院系信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
str = richdepartment.Text.Trim().Replace("''","'");
string[] str1 = boperate.Split(str,",");
if (cn.State == ConnectionState.Closed)
cn.Open();
for (int i = 0; i < str1.Length; i++)
{
SqlCommand cmd = new SqlCommand("select * from department where department = '" + str1[i].ToString().Trim() + "'", cn);
if (null != cmd.ExecuteScalar())
MessageBox.Show("表中已存在该院系", "提示");
else
{
if (str1[i] != "")
{
sql = "insert into department(department)values('" + str1[i].Trim() + "')";
boperate.getcom(sql);
}
}
}
MessageBox.Show("院系信息添加成功", "提示");
WeihuDepartmen_Load(sender, e);
richdepartment.Clear();
if (cn.State == ConnectionState.Open)
cn.Close();
}
#endregion
#region 删除院系信息
/// <summary>
/// 删除院系信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("如果删除该院系,相应专业、班级、表中的信息也将被删除,确定要删除选中的院系吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
boperate.getcom("delete from department where department='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
WeihuDepartmen_Load(sender, e);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
#endregion
#region 关闭
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBack_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region 专业标签中选择院系
/// <summary>
/// 显示与combox中相同的院系
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void combBdepartment_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet myds = boperate.getds("select department as 院系名称,profession as 专业 from profession where department = '" + combBdepartment.Text.ToString().Trim() + "'", M_str_table1);
dataGridView2.DataSource = myds.Tables[0];
if (myds.Tables[0].Rows.Count > 0)
btnpDelete.Enabled = true;
else
btnpDelete.Enabled = false;
}
#endregion
#region 添加专业
/// <summary>
/// 添加专业信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnpAdd_Click(object sender, EventArgs e)
{
str = richprofession.Text.Trim().Replace("''", "'");
string[] str1 = boperate.Split(str,",");
if (cn.State == ConnectionState.Closed)
cn.Open();
for (int i = 0; i < str1.Length; i++)
{
SqlCommand cmd = new SqlCommand("select * from Profession where Profession = '" + str1[i].ToString().Trim() + "'", cn);
if (null != cmd.ExecuteScalar())
MessageBox.Show("表中已存在该专业", "提示");
else
{
if (str1[i] != "")
{
sql = "insert into Profession(Profession,department)values('" + str1[i].Trim() + "','" + combBdepartment.Text.Trim() + "')";
boperate.getcom(sql);
}
}
}
MessageBox.Show("专业信息添加成功", "提示");
combBdepartment_SelectedIndexChanged(sender, e);
richprofession.Clear();
if (cn.State == ConnectionState.Open)
cn.Close();
}
#endregion
#region 删除专业信息
/// <summary>
/// 删除专业信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnpDelete_Click(object sender, EventArgs e)
{
string profession1 = Convert.ToString(dataGridView2[1, dataGridView2.CurrentCell.RowIndex].Value).Trim();
string s = "delete from Profession where Profession = '";
try
{
DataSet myds = boperate.getds("select * from student where profession = '" + profession1 + "'", "student");
if (myds.Tables[0].Rows.Count > 0)
{
MessageBox.Show("该专业中已存在学生信息,不允许清除。如果要清除专业信息,请先清除相应班的学生信息");
}
else
{
if (MessageBox.Show("如果删除该专业,相应班级表中的信息也将被删除,确定要删除选中的专业吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
boperate.getcom(s + profession1 + "'");
MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
combBdepartment_SelectedIndexChanged(sender, e);
}
#endregion
#region 关闭
/// <summary>
/// 关闭院系信息维护对话框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnpQuit_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region 变换标签时加载相应的信息
/// <summary>
/// 变换标签时加载相应的信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
//加载专业表中的信息
DataSet myds = boperate.getds(M_str_sql1, M_str_table1);
dataGridView2.DataSource = myds.Tables[0];
if (myds.Tables[0].Rows.Count > 0)
btnpDelete.Enabled = true;
else
btnpDelete.Enabled = false;
//combBdepartment中加载院系信息
opAndvalidate.cboxBind("select department from department", "department", "department", combBdepartment);
//加载班级表中的信息
myds = boperate.getds(M_str_sql2, M_str_table2);
dataGridView3.DataSource = myds.Tables[0];
if (myds.Tables[0].Rows.Count > 0)
btnCDelete.Enabled = true;
else
btnCDelete.Enabled = false;
//combBdepartment中加载院系信息
opAndvalidate.cboxBind("select department from department", "department", "department", comCDepartment);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -