📄 browseclass.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// bjll 的摘要说明。
/// </summary>
public class BrowseClass : System.Windows.Forms.Form
{
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public BrowseClass()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "Data Source=xsxxdb.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet." +
"OLEDB.4.0;";
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.comboBox1.ForeColor = System.Drawing.SystemColors.ControlText;
this.comboBox1.Location = new System.Drawing.Point(149, 21);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(171, 23);
this.comboBox1.TabIndex = 1;
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox1.ForeColor = System.Drawing.SystemColors.ControlText;
this.textBox1.Location = new System.Drawing.Point(469, 21);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 25);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "2004";
//
// dataGrid1
//
this.dataGrid1.AllowSorting = false;
this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.ActiveBorder;
this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.Highlight;
this.dataGrid1.DataMember = "";
this.dataGrid1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 62);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.PreferredColumnWidth = 80;
this.dataGrid1.PreferredRowHeight = 30;
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(832, 267);
this.dataGrid1.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.label1.Location = new System.Drawing.Point(32, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(83, 21);
this.label1.TabIndex = 4;
this.label1.Text = "请选择专业";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.ForeColor = System.Drawing.SystemColors.ControlText;
this.label2.Location = new System.Drawing.Point(373, 23);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(67, 21);
this.label2.TabIndex = 5;
this.label2.Text = "入学年份";
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.ForeColor = System.Drawing.SystemColors.ControlText;
this.button1.Location = new System.Drawing.Point(619, 19);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(100, 30);
this.button1.TabIndex = 6;
this.button1.Text = "开始查询";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(128, 339);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(117, 30);
this.button2.TabIndex = 7;
this.button2.Text = "修改班级信息";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(587, 339);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(100, 30);
this.button3.TabIndex = 8;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(373, 339);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(118, 30);
this.button4.TabIndex = 9;
this.button4.Text = "删除班级信息";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// BrowseClass
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(829, 384);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.comboBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "BrowseClass";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "班级信息浏览";
this.Load += new System.EventHandler(this.BrowseClass_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void BrowseClass_Load(object sender, System.EventArgs e)
{
//产生专业下拉列表
string sql="select specialtyid,specialtyname from specialtyinfo";
OleDbDataAdapter adp=new OleDbDataAdapter(sql,oleDbConnection1);
oleDbConnection1.Open();
DataSet ds=new DataSet();
adp.Fill(ds,"specialty");
comboBox1.DataSource=ds.Tables["specialty"].DefaultView;
comboBox1.DisplayMember="specialtyname";
comboBox1.ValueMember="specialtyid";
}
public bool strisyear(string s)
{
if (s.Trim().Length!=4)
return (false);
else
{
int n=1;
for (int i=0;i<=s.Length-1;i++)
{
if (!System.Char.IsNumber(s[i]))
{
n=-1;
break;
}
}
if (n==1)
return (true);
else
return (false);
}
}
DataSet ds;
private void button1_Click(object sender, System.EventArgs e)
{
if (!strisyear(textBox1.Text.Trim()))
MessageBox.Show("请输入合法的年份","错误");
else
{
string sql="select classid as 班级编号,classname as 班级名称,specialtyname as 专业名称,classnumber as 教室位置,length as 年制,teacher as 办主任,enrolmenttime as 入校时间,remark as 备注 from classinfo where specialtyname='"+comboBox1.Text.ToString()+"' and year(enrolmenttime)="+textBox1.Text.ToString();
OleDbDataAdapter adp=new OleDbDataAdapter(sql,oleDbConnection1);
ds=new DataSet();
ds.Clear();
adp.Fill(ds,"class");
if (ds.Tables[0].Rows.Count!=0)
{
dataGrid1.DataSource=ds.Tables[0].DefaultView;
dataGrid1.CaptionText="共有"+ds.Tables[0].Rows.Count+"条查询结果";
}
else
{
dataGrid1.CaptionText="没有您所查找的班级信息";
dataGrid1.DataSource=null;
}
}
}
public ModifyClass modifyclass;
private void button2_Click(object sender, System.EventArgs e)
{
if (dataGrid1.DataSource!=null && dataGrid1[dataGrid1.CurrentCell]!=null)
{
modifyclass=new ModifyClass();
modifyclass.Tag=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim();
modifyclass.textBox1.Text=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim();
modifyclass.label9.Text=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][2].ToString().Trim();
modifyclass.textBox3.Text=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][3].ToString().Trim();
modifyclass.numericUpDown1.Value=int.Parse(ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][4].ToString().Trim());
modifyclass.textBox5.Text=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][5].ToString().Trim();
modifyclass.dateTimePicker1.Text=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][6].ToString().Trim();
modifyclass.textBox2.Text=ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][7].ToString().Trim();
modifyclass.ShowDialog();
}
else
MessageBox.Show("您所选定的班级无效","提示");
}
private void button3_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button4_Click(object sender, System.EventArgs e)
{
if (dataGrid1.DataSource!=null && dataGrid1[dataGrid1.CurrentCell]!=null)
{
string sql="select classname from classinfo where classid="+ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+" and classid not in (select distinct classid from studentinfo)";
OleDbCommand cmd=new OleDbCommand(sql,oleDbConnection1);
OleDbDataReader dr;
dr=cmd.ExecuteReader();
if (!dr.Read())
{
MessageBox.Show("删除班级'"+ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim()+"'失败,请先删除该班级的学生","提示");
dr.Close();
}
else
{
dr.Close();
//首先删除与班级相关的课程
sql="delete * from courseinfo where classid="+ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim();
cmd.CommandText=sql;
cmd.ExecuteNonQuery();
//然后删除所指定的班级
sql="delete * from classinfo where classid="+ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim();
cmd.CommandText=sql;
cmd.ExecuteNonQuery();
MessageBox.Show("删除班级'"+ds.Tables["class"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim()+"'成功","提示");
}
}
else
MessageBox.Show("您所选定的班级无效","提示");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -