?? 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>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public BrowseClass()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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 = "請選擇專業(yè)";
//
// 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 = "入學(xué)年份";
//
// 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)
{
//產(chǎn)生專業(yè)下拉列表
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 專業(yè)名稱,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+"條查詢結(jié)果";
}
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()+"'失敗,請先刪除該班級的學(xué)生","提示");
dr.Close();
}
else
{
dr.Close();
//首先刪除與班級相關(guān)的課程
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("您所選定的班級無效","提示");
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -