⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collegeform.cs

📁 《数据库原理及应用》-刘玉宝主编-源代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace KTMIS
{
	/// <summary>
	/// colege 的摘要说明。
	/// </summary>
	public class college : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button returnBtn;
		private System.Windows.Forms.DataGrid collegedataGrid;
		private System.Windows.Forms.Label collegelabel;
		private System.Windows.Forms.TextBox collegetextBox;
		private System.Windows.Forms.Button selectBtn;
		private System.Windows.Forms.Button deleteBtn;
		private System.Windows.Forms.Button updateBtn;
		private System.Windows.Forms.Button addBtn;

		private SqlConnection collegeConnection = null;
		private SqlCommand collegeSelectCommand = null;
		private SqlCommand collegeDeleteCommand = null;
		private SqlDataAdapter collegeDataAdapter = null;
		private DataSet ds = null;
		private string sqlString = null; 
		private int Row = -1;
		private System.Windows.Forms.Label colegelabel;
		private System.Windows.Forms.TextBox colegetextBox;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public college()
		{
			//
			// 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.returnBtn = new System.Windows.Forms.Button();
			this.collegedataGrid = new System.Windows.Forms.DataGrid();
			this.colegelabel = new System.Windows.Forms.Label();
			this.colegetextBox = new System.Windows.Forms.TextBox();
			this.selectBtn = new System.Windows.Forms.Button();
			this.deleteBtn = new System.Windows.Forms.Button();
			this.updateBtn = new System.Windows.Forms.Button();
			this.addBtn = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.collegedataGrid)).BeginInit();
			this.SuspendLayout();
			// 
			// returnBtn
			// 
			this.returnBtn.Location = new System.Drawing.Point(544, 8);
			this.returnBtn.Name = "returnBtn";
			this.returnBtn.Size = new System.Drawing.Size(120, 32);
			this.returnBtn.TabIndex = 23;
			this.returnBtn.Text = "返回";
			this.returnBtn.Click += new System.EventHandler(this.returnBtn_Click);
			// 
			// collegedataGrid
			// 
			this.collegedataGrid.DataMember = "";
			this.collegedataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.collegedataGrid.Location = new System.Drawing.Point(16, 104);
			this.collegedataGrid.Name = "collegedataGrid";
			this.collegedataGrid.ReadOnly = true;
			this.collegedataGrid.Size = new System.Drawing.Size(664, 328);
			this.collegedataGrid.TabIndex = 22;
			this.collegedataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.colegedataGrid_MouseDown);
			this.collegedataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.colegedataGrid_MouseUp);
			// 
			// colegelabel
			// 
			this.colegelabel.Location = new System.Drawing.Point(24, 56);
			this.colegelabel.Name = "colegelabel";
			this.colegelabel.Size = new System.Drawing.Size(256, 24);
			this.colegelabel.TabIndex = 24;
			this.colegelabel.Text = "请输入所要查询的学院名称:";
			// 
			// colegetextBox
			// 
			this.colegetextBox.Location = new System.Drawing.Point(312, 56);
			this.colegetextBox.Name = "colegetextBox";
			this.colegetextBox.Size = new System.Drawing.Size(184, 30);
			this.colegetextBox.TabIndex = 25;
			this.colegetextBox.Text = "";
			// 
			// selectBtn
			// 
			this.selectBtn.Location = new System.Drawing.Point(536, 56);
			this.selectBtn.Name = "selectBtn";
			this.selectBtn.Size = new System.Drawing.Size(96, 32);
			this.selectBtn.TabIndex = 19;
			this.selectBtn.Text = "查询";
			this.selectBtn.Click += new System.EventHandler(this.selectBtn_Click);
			// 
			// deleteBtn
			// 
			this.deleteBtn.Location = new System.Drawing.Point(368, 8);
			this.deleteBtn.Name = "deleteBtn";
			this.deleteBtn.Size = new System.Drawing.Size(120, 32);
			this.deleteBtn.TabIndex = 18;
			this.deleteBtn.Text = "删除";
			this.deleteBtn.Click += new System.EventHandler(this.deleteBtn_Click);
			// 
			// updateBtn
			// 
			this.updateBtn.Location = new System.Drawing.Point(200, 8);
			this.updateBtn.Name = "updateBtn";
			this.updateBtn.Size = new System.Drawing.Size(120, 32);
			this.updateBtn.TabIndex = 17;
			this.updateBtn.Text = "修改";
			this.updateBtn.Click += new System.EventHandler(this.updateBtn_Click);
			// 
			// addBtn
			// 
			this.addBtn.Location = new System.Drawing.Point(32, 8);
			this.addBtn.Name = "addBtn";
			this.addBtn.Size = new System.Drawing.Size(120, 32);
			this.addBtn.TabIndex = 16;
			this.addBtn.Text = "添加";
			this.addBtn.Click += new System.EventHandler(this.addBtn_Click);
			// 
			// college
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(11, 23);
			this.ClientSize = new System.Drawing.Size(696, 454);
			this.Controls.Add(this.returnBtn);
			this.Controls.Add(this.collegedataGrid);
			this.Controls.Add(this.colegelabel);
			this.Controls.Add(this.colegetextBox);
			this.Controls.Add(this.selectBtn);
			this.Controls.Add(this.deleteBtn);
			this.Controls.Add(this.updateBtn);
			this.Controls.Add(this.addBtn);
			this.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.Name = "college";
			this.Text = "院系信息维护";
			this.Load += new System.EventHandler(this.colege_Load);
			((System.ComponentModel.ISupportInitialize)(this.collegedataGrid)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void colegedataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			DataGrid myGrid = (DataGrid) sender;
			DataGrid.HitTestInfo hti;
			hti = myGrid.HitTest(e.X, e.Y);
			if(hti.Type==DataGrid.HitTestType.Cell)
			{
				Row = hti.Row;
			}
			else
			{
				Row = -1;
			}
		}

		private void colegedataGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(Row!=-1)
			{
				collegedataGrid.Select(Row);
			}
		}

		private void colege_Load(object sender, System.EventArgs e)
		{
			//string connString = "user id=sa;data source=\".\";initial catalog=KTMIS";
			//collegeConnection = new SqlConnection(connString);
			collegeSelectCommand = new SqlCommand("",collegeConnection);
			collegeDeleteCommand = new SqlCommand("DeleteCollege",collegeConnection); 
			sqlString = "select CollegeNa as 院系名称 from College"; 
			ds = new DataSet();
			try
			{
				collegeDataAdapter = new SqlDataAdapter(sqlString,collegeConnection);
				collegeDataAdapter.Fill(ds,"college");
			}
			catch(Exception ex)
			{
				MessageBox.Show("载入错误!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			collegedataGrid.SetDataBinding(ds,"college");
		}

		private void Initial()
		{
			ds.Clear();
			collegeDataAdapter.SelectCommand.CommandText = sqlString;
			try
			{
				collegeConnection.Open();
				collegeDataAdapter.Fill(ds,"college");
				collegedataGrid.SetDataBinding(ds,"college");
			}
			catch(Exception e)
			{
				MessageBox.Show("初始化错误!原因是"+e.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			finally
			{
				collegeConnection.Close();
			}
		}

		private void addBtn_Click(object sender, System.EventArgs e)
		{
			AddCollege ac = new AddCollege();
			ac.Connection = this.collegeConnection;
			ac.ShowDialog();
			Initial();
		}

		private void updateBtn_Click(object sender, System.EventArgs e)
		{
			string name = null;
			DataTable dt = ds.Tables["college"];
			for(int i = 0;i<dt.Rows.Count;i++)
			{
				if(collegedataGrid.IsSelected(i))
				{
					name = dt.Rows[i][0].ToString();
					break;
				}
			}
			if(name==null)
			{
				MessageBox.Show("请选择在表框中所要修改的分类的行!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			string collegeCo = null;
			string selectString = "select CollegeCo from college where CollegeNa=\'"+name+"\'";
			collegeSelectCommand.CommandText = selectString;
			try
			{
				collegeConnection.Open();
				SqlDataReader dr = collegeSelectCommand.ExecuteReader();
				while(dr.Read())
				{
					collegeCo = dr.GetValue(0).ToString();
					break;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(" 读取数据失败!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			finally
			{
				collegeConnection.Close();
			}
			if(collegeCo==null)
			{
				MessageBox.Show("操作失败,请重试!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			AddCollege ac = new AddCollege();
			ac.SetcollegeBtnText("修改");
			ac.SetcollegeCoText(collegeCo.ToString());
			ac.SetcollegeSNText(name);
			ac.SetFormTital("修改院系");
			ac.ShowDialog();
			Initial();
		}

		private void deleteBtn_Click(object sender, System.EventArgs e)
		{
			string questionStr = "若删除该院系信息,与其相关的课题信息亦将同时被删除!\n\n是否继续?";
			DialogResult dialogResult = MessageBox.Show(questionStr, "系统提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			if(dialogResult == DialogResult.No)
			{
				return;
			}

			string name = null;
			DataTable dt = ds.Tables["college"];
			for(int i = 0;i<dt.Rows.Count;i++)
			{
				if(collegedataGrid.IsSelected(i))
				{
					name = dt.Rows[i][0].ToString();
					break;
				}
			}
			if(name==null)
			{
				MessageBox.Show("请选择在表框中所要修改的分类的行!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			string collegeCo = null;
			string selectString = "select CollegeCo from College where CollegeNa=\'"+name+"\'";
			collegeSelectCommand.CommandText = selectString;
			try
			{
				collegeConnection.Open();
				SqlDataReader dr = collegeSelectCommand.ExecuteReader();
				while(dr.Read())
				{
					collegeCo = dr.GetValue(0).ToString();
					break;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(" 读取数据失败!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			finally
			{
				collegeConnection.Close();
			}
			if(collegeCo==null)
			{
				MessageBox.Show("操作失败,请重试!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			collegeDeleteCommand.CommandType = CommandType.StoredProcedure;
			collegeDeleteCommand.Parameters.Clear();
			collegeDeleteCommand.Parameters.Add("@CollegeCo",SqlDbType.Int);
			collegeDeleteCommand.Parameters["@CollegeCo"].Value = Convert.ToInt16(collegeCo.Trim());
			try
			{
				collegeConnection.Open();
				collegeDeleteCommand.ExecuteNonQuery();
				MessageBox.Show("删除成功!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch(Exception ex)
			{
				MessageBox.Show("删除失败,原因是:"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
				return;
			}
			finally
			{
				collegeConnection.Close();
			}
			
			Initial();
		}

		private void returnBtn_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void selectBtn_Click(object sender, System.EventArgs e)
		{
			string sqlStr = "select CollegeNa as 院系名称 from College where CollegeNa like \'%" +collegetextBox.Text.Trim()+"%\'"; 
			ds.Clear();
			try
			{
				collegeDataAdapter.SelectCommand.CommandText = sqlStr;
				collegeDataAdapter.Fill(ds,"college");
			}
			catch(Exception ex)
			{
				MessageBox.Show("载入错误!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			collegedataGrid.SetDataBinding(ds,"college");
		}

		public System.Data.SqlClient.SqlConnection Connection
		{
			set
			{
				this.collegeConnection = value;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -