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

📄 adddep.cs

📁 学生信息管理系统: 采用C#编写
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace Students
{
	/// <summary>
	/// AddDep 的摘要说明。
	/// </summary>
	public class AddDep : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button DepInfo;
		private System.Windows.Forms.Label label2;
		private string  connectstr = "data source=WJD;Initial Catalog=students;integrated security=SSPI;";
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public AddDep()
		{
			//
			// 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.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.DepInfo = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(112, 144);
			this.textBox4.Name = "textBox4";
			this.textBox4.TabIndex = 21;
			this.textBox4.Text = "";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(112, 104);
			this.textBox3.Name = "textBox3";
			this.textBox3.TabIndex = 20;
			this.textBox3.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(112, 64);
			this.textBox2.Name = "textBox2";
			this.textBox2.TabIndex = 19;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(112, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 18;
			this.textBox1.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(72, 144);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(32, 23);
			this.label5.TabIndex = 17;
			this.label5.Text = "备注";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(72, 104);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(32, 23);
			this.label4.TabIndex = 16;
			this.label4.Text = "院长";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(48, 64);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 23);
			this.label3.TabIndex = 15;
			this.label3.Text = "学院名称";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// DepInfo
			// 
			this.DepInfo.Location = new System.Drawing.Point(32, 184);
			this.DepInfo.Name = "DepInfo";
			this.DepInfo.Size = new System.Drawing.Size(88, 23);
			this.DepInfo.TabIndex = 13;
			this.DepInfo.Text = "添  加";
			this.DepInfo.Click += new System.EventHandler(this.DepInfo_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(48, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 14;
			this.label2.Text = "学院编号";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(152, 184);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(88, 23);
			this.button1.TabIndex = 22;
			this.button1.Text = "退  出";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// AddDep
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.DepInfo);
			this.Controls.Add(this.label2);
			this.Name = "AddDep";
			this.Text = "添加学院信息";
			this.ResumeLayout(false);

		}
		#endregion

		private void DepInfo_Click(object sender, System.EventArgs e)
		{
			int Dep_Num;
			string Dep_Name;
			string Dep_Head;

			//判断必须输入的信息已输入之后在给变量赋值
			if( (textBox1.Text!="") && (textBox2.Text!="") && (textBox3.Text!=""))
			{
				Dep_Num = Int32.Parse(textBox1.Text);
				Dep_Name = textBox2.Text;
				Dep_Head = textBox3.Text;
			}
			else
			{
				MessageBox.Show("请输入完整的信息!");
				return;
			}
			
			string Dep_Des = textBox4.Text;
			//向数据库中插入一条记录的语句
			string sql = String.Format("insert into Department (Dep_Number,Dep_Name,Dep_Head,Dep_Des) values ({0},'{1}','{2}','{3}')",Dep_Num,Dep_Name,Dep_Head,Dep_Des);
			SqlConnection sqlcnn = new SqlConnection( connectstr );
			try
			{
				//打开数据库
				sqlcnn.Open();
				SqlCommand cmd = new SqlCommand(sql, sqlcnn);
				//执行插入操作,并返回受影响的行数
				int i = cmd.ExecuteNonQuery();
				//如果受影响的行数小于一则说明操作失败了
				if ( i<1 )
					MessageBox.Show("操作失败!");
				//关闭数据库
				sqlcnn.Close();
			}
			catch(Exception er)
			{
				string temp = er.Message.ToString();
				return;
			}
			MessageBox.Show("录入成功!");
		}

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


	}
}

⌨️ 快捷键说明

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