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

📄 sortform.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>
	/// Sort 的摘要说明。
	/// </summary>
	public class Sort : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid SortdataGrid;
		private SqlConnection SortConnection = null;
		private SqlCommand SortSelectCommand = null;
		private SqlCommand SortDeleteCommand = null;
		private SqlDataAdapter SortDataAdapter = null;
		private DataSet ds = null;
		private string sqlString = null; 
		private int Row = -1;
		private System.Windows.Forms.Button addBtn;
		private System.Windows.Forms.Button updateBtn;
		private System.Windows.Forms.Button deleteBtn;
		private System.Windows.Forms.Button selectBtn;
		private System.Windows.Forms.TextBox FLMtextBox;
		private System.Windows.Forms.Label FLMlabel;
		private System.Windows.Forms.Button returnBtn;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Sort()
		{
			//
			// 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.addBtn = new System.Windows.Forms.Button();
			this.updateBtn = new System.Windows.Forms.Button();
			this.deleteBtn = new System.Windows.Forms.Button();
			this.selectBtn = new System.Windows.Forms.Button();
			this.FLMtextBox = new System.Windows.Forms.TextBox();
			this.FLMlabel = new System.Windows.Forms.Label();
			this.SortdataGrid = new System.Windows.Forms.DataGrid();
			this.returnBtn = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.SortdataGrid)).BeginInit();
			this.SuspendLayout();
			// 
			// addBtn
			// 
			this.addBtn.Location = new System.Drawing.Point(32, 32);
			this.addBtn.Name = "addBtn";
			this.addBtn.Size = new System.Drawing.Size(120, 32);
			this.addBtn.TabIndex = 0;
			this.addBtn.Text = "添加";
			this.addBtn.Click += new System.EventHandler(this.addBtn_Click);
			// 
			// updateBtn
			// 
			this.updateBtn.Location = new System.Drawing.Point(200, 32);
			this.updateBtn.Name = "updateBtn";
			this.updateBtn.Size = new System.Drawing.Size(120, 32);
			this.updateBtn.TabIndex = 1;
			this.updateBtn.Text = "修改";
			this.updateBtn.Click += new System.EventHandler(this.updateBtn_Click);
			// 
			// deleteBtn
			// 
			this.deleteBtn.Location = new System.Drawing.Point(368, 32);
			this.deleteBtn.Name = "deleteBtn";
			this.deleteBtn.Size = new System.Drawing.Size(120, 32);
			this.deleteBtn.TabIndex = 2;
			this.deleteBtn.Text = "删除";
			this.deleteBtn.Click += new System.EventHandler(this.deleteBtn_Click);
			// 
			// selectBtn
			// 
			this.selectBtn.Location = new System.Drawing.Point(536, 80);
			this.selectBtn.Name = "selectBtn";
			this.selectBtn.Size = new System.Drawing.Size(96, 32);
			this.selectBtn.TabIndex = 3;
			this.selectBtn.Text = "查询";
			this.selectBtn.Click += new System.EventHandler(this.selectBtn_Click);
			// 
			// FLMtextBox
			// 
			this.FLMtextBox.Location = new System.Drawing.Point(312, 80);
			this.FLMtextBox.Name = "FLMtextBox";
			this.FLMtextBox.Size = new System.Drawing.Size(200, 30);
			this.FLMtextBox.TabIndex = 4;
			this.FLMtextBox.Text = "";
			// 
			// FLMlabel
			// 
			this.FLMlabel.Location = new System.Drawing.Point(24, 80);
			this.FLMlabel.Name = "FLMlabel";
			this.FLMlabel.Size = new System.Drawing.Size(280, 32);
			this.FLMlabel.TabIndex = 5;
			this.FLMlabel.Text = "请输入所要查询的分类名:";
			// 
			// SortdataGrid
			// 
			this.SortdataGrid.DataMember = "";
			this.SortdataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.SortdataGrid.Location = new System.Drawing.Point(16, 128);
			this.SortdataGrid.Name = "SortdataGrid";
			this.SortdataGrid.ReadOnly = true;
			this.SortdataGrid.Size = new System.Drawing.Size(664, 328);
			this.SortdataGrid.TabIndex = 6;
			this.SortdataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SortdataGrid_MouseDown);
			this.SortdataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.SortdataGrid_MouseUp);
			// 
			// returnBtn
			// 
			this.returnBtn.Location = new System.Drawing.Point(544, 32);
			this.returnBtn.Name = "returnBtn";
			this.returnBtn.Size = new System.Drawing.Size(120, 32);
			this.returnBtn.TabIndex = 7;
			this.returnBtn.Text = "返回";
			this.returnBtn.Click += new System.EventHandler(this.returnBtn_Click);
			// 
			// Sort
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(11, 23);
			this.ClientSize = new System.Drawing.Size(696, 470);
			this.Controls.Add(this.returnBtn);
			this.Controls.Add(this.SortdataGrid);
			this.Controls.Add(this.FLMlabel);
			this.Controls.Add(this.FLMtextBox);
			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 = "Sort";
			this.Text = "分类信息维护";
			this.Load += new System.EventHandler(this.Sort_Load);
			((System.ComponentModel.ISupportInitialize)(this.SortdataGrid)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Sort_Load(object sender, System.EventArgs e)
		{ 
			//string connString = "user id=sa;data source=\".\";initial catalog=KTMIS";
			//SortConnection = new SqlConnection(connString);
			SortSelectCommand = new SqlCommand("",SortConnection);
			SortDeleteCommand = new SqlCommand("DeleteSort",SortConnection); 
			sqlString = "select SortNa as 分类名称,SortPY as 分类拼音缩写 from Sort"; 
			ds = new DataSet();
			try
			{
				SortDataAdapter = new SqlDataAdapter(sqlString,SortConnection);
				SortDataAdapter.Fill(ds,"Sort");
			}
			catch(Exception ex)
			{
				MessageBox.Show("载入错误!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			SortdataGrid.SetDataBinding(ds,"Sort");
		}


		private void addBtn_Click(object sender, System.EventArgs e)
		{
			addSort aSort = new addSort();
			aSort.Connection = this.SortConnection;
			aSort.ShowDialog();
			Initial();
		}

		private void updateBtn_Click(object sender, System.EventArgs e)
		{
			string name = null;
			string py = null;
			DataTable dt = ds.Tables["Sort"];
			for(int i = 0;i<dt.Rows.Count;i++)
			{
				if(SortdataGrid.IsSelected(i))
				{
					name = dt.Rows[i][0].ToString();
					py = dt.Rows[i][1].ToString();
					break;
				}
			}
			if(name==null)
			{
				MessageBox.Show("请选择在表框中所要修改的分类的行!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			string sortCo = null;
			string selectString = "select SortCo from Sort where SortNa=\'"+name+"\' and SortPY=\'"+py+"\'";
			SortSelectCommand.CommandText = selectString;
			try
			{
				SortConnection.Open();
				SqlDataReader dr = SortSelectCommand.ExecuteReader();
				while(dr.Read())
				{
					sortCo = dr.GetValue(0).ToString();
					break;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(" 读取数据失败!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			finally
			{
				SortConnection.Close();
			}
			if(sortCo==null)
			{
				MessageBox.Show("操作失败,请重试!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			addSort aSort = new addSort();
			aSort.SetBtnText("修改");
			aSort.SetSortCoText(sortCo.ToString());
			aSort.SetSNText(name);
			aSort.SetSNPYText(py);
			aSort.SetFormTital("修改分类");
			aSort.ShowDialog();
			Initial();
		}

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

		private void SortdataGrid_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 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;
			string py = null;
			DataTable dt = ds.Tables["Sort"];
			for(int i = 0;i<dt.Rows.Count;i++)
			{
				if(SortdataGrid.IsSelected(i))
				{
					name = dt.Rows[i][0].ToString();
					py = dt.Rows[i][1].ToString();
					break;
				}
			}
			if(name==null)
			{
				MessageBox.Show("请选择在表框中所要修改的分类的行!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			string sortCo = null;
			string selectString = "select SortCo from Sort where SortNa=\'"+name+"\' and SortPY=\'"+py+"\'";
			SortSelectCommand.CommandText = selectString;
			try
			{
				SortConnection.Open();
				SqlDataReader dr = SortSelectCommand.ExecuteReader();
				while(dr.Read())
				{
					sortCo = dr.GetValue(0).ToString();
					break;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(" 读取数据失败!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			finally
			{
				SortConnection.Close();
			}
			if(sortCo==null)
			{
				MessageBox.Show("操作失败,请重试!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			SortDeleteCommand.CommandType = CommandType.StoredProcedure;
			SortDeleteCommand.Parameters.Clear();
			SortDeleteCommand.Parameters.Add("@SortCo",SqlDbType.Int);
			SortDeleteCommand.Parameters["@SortCo"].Value = Convert.ToInt16(sortCo.Trim());
			try
			{
				SortConnection.Open();
				SortDeleteCommand.ExecuteNonQuery();
				MessageBox.Show("删除成功!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch(Exception ex)
			{
				MessageBox.Show("删除失败,原因是:"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
				return;
			}
			finally
			{
				SortConnection.Close();
			}
			
			Initial();
		}

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

		private void selectBtn_Click(object sender, System.EventArgs e)
		{
			string sqlStr = "select SortNa as 分类名称,SortPY as 分类拼音缩写 from Sort where SortNa like \'%" +FLMtextBox.Text.Trim()+"%\'"; 
			ds.Clear();
			try
			{
				SortDataAdapter.SelectCommand.CommandText = sqlStr;
				SortDataAdapter.Fill(ds,"Sort");
			}
			catch(Exception ex)
			{
				MessageBox.Show("载入错误!原因是"+ex.ToString(),"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			SortdataGrid.SetDataBinding(ds,"Sort");
		}

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

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

⌨️ 快捷键说明

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