📄 addstu.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>
/// AddStu 的摘要说明。
/// </summary>
public class AddStu : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox9;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button StuInfo;
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 AddStu()
{
//
// 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.textBox9 = new System.Windows.Forms.TextBox();
this.textBox8 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.StuInfo = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox9
//
this.textBox9.Location = new System.Drawing.Point(128, 158);
this.textBox9.Name = "textBox9";
this.textBox9.TabIndex = 24;
this.textBox9.Text = "";
//
// textBox8
//
this.textBox8.Location = new System.Drawing.Point(128, 118);
this.textBox8.Name = "textBox8";
this.textBox8.TabIndex = 23;
this.textBox8.Text = "";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(128, 78);
this.textBox6.Name = "textBox6";
this.textBox6.TabIndex = 22;
this.textBox6.Text = "";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(128, 38);
this.textBox5.Name = "textBox5";
this.textBox5.TabIndex = 21;
this.textBox5.Text = "";
//
// label10
//
this.label10.Location = new System.Drawing.Point(64, 158);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(56, 23);
this.label10.TabIndex = 20;
this.label10.Text = "学院编号";
this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label9
//
this.label9.Location = new System.Drawing.Point(88, 118);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(32, 23);
this.label9.TabIndex = 19;
this.label9.Text = "年龄";
this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label7
//
this.label7.Location = new System.Drawing.Point(64, 78);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(56, 23);
this.label7.TabIndex = 18;
this.label7.Text = "学生姓名";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label6
//
this.label6.Location = new System.Drawing.Point(80, 38);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(40, 23);
this.label6.TabIndex = 17;
this.label6.Text = "学号";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// StuInfo
//
this.StuInfo.Location = new System.Drawing.Point(56, 200);
this.StuInfo.Name = "StuInfo";
this.StuInfo.Size = new System.Drawing.Size(88, 23);
this.StuInfo.TabIndex = 16;
this.StuInfo.Text = "添 加";
this.StuInfo.Click += new System.EventHandler(this.StuInfo_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 25;
this.button1.Text = "退 出";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// AddStu
//
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.textBox9);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.StuInfo);
this.Name = "AddStu";
this.Text = "添加学生信息";
this.ResumeLayout(false);
}
#endregion
private void StuInfo_Click(object sender, System.EventArgs e)
{
string Stu_Number;
string Stu_Name;
int Stu_Age;
int Dep_Number;
//判断必须输入的信息已输入之后在给变量赋值
if( (textBox5.Text!="") && (textBox6.Text!="") && (textBox8.Text!="") &&(textBox9.Text!=""))
{
Stu_Number = textBox5.Text;
Stu_Name = textBox6.Text;
Stu_Age = Int32.Parse(textBox8.Text);
Dep_Number = Int32.Parse(textBox9.Text);
}
else
{
MessageBox.Show("请输入完整的信息!");
return;
}
string check = String.Format("select Dep_Number from Department where Dep_Number={0}", Dep_Number);
string sql = String.Format("insert into Students (Stu_Number,Stu_Name,Stu_Age,Dep_Number) values ('{0}','{1}',{2},{3})",Stu_Number,Stu_Name,Stu_Age,Dep_Number);
SqlConnection sqlcnn = new SqlConnection( connectstr );
try
{
sqlcnn.Open();
SqlCommand cmd = new SqlCommand(check, sqlcnn);
//查询Dep_Number表中是否存在当前要录入的学生所在的学院
SqlDataReader MyDataReader = cmd.ExecuteReader();
if ( !MyDataReader.Read() )
{
MessageBox.Show("目前还没有这个学院!");
MyDataReader.Close();
sqlcnn.Close();
return;
}
MyDataReader.Close();
SqlCommand cmd1 = new SqlCommand(sql, sqlcnn);
//执行插入操作,并返回受影响的行数
int j = cmd1.ExecuteNonQuery();
//如果受影响的行数小于一则说明操作失败了
if ( j<1 )
{
MessageBox.Show("操作失败!");
sqlcnn.Close();
}
sqlcnn.Close();
}
catch(Exception er)
{
string temp = er.Message.ToString();
sqlcnn.Close();
return;
}
MessageBox.Show("录入成功!");
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -