📄 addstu.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class addstu : Form
{
baseclass.BaseOperate boperate = new baseclass.BaseOperate();
baseclass.OperateAndValidate opAndvalidate = new baseclass.OperateAndValidate();
SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
ProctStuInfo pstu = new ProctStuInfo();
string TaskClass;
//构造函数重载
public addstu(string TaskClass)
{
InitializeComponent();
this.TaskClass = TaskClass;
}
public addstu()
{
InitializeComponent();
}
/// <确定>
/// 点击确定进行添加或更新
/// </确定>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOk_Click(object sender, EventArgs e)
{
string sql1, sql2,sql;
if (txtStuNum.Text.Trim() == "" || txtStuName.Text.Trim() == "" || comBDeparture.Text.Trim() == "" || comBClass.Text.Trim() == "")
MessageBox.Show("请填写完整的学生信息", "提示");
else
{
if (!opAndvalidate.validateNum(txtStuNum.Text.Trim()))
errorPnum.SetError(txtStuNum, "请输入正整数");
else
{
errorPnum.Clear();
if (Ifenogh())
{
if (this.TaskClass.Equals("Add"))
{
SqlCommand cmd = new SqlCommand("select * from student where S_number='" + txtStuNum.Text.Trim() + "'", cn);
if(cn.State == ConnectionState.Closed)
cn.Open();
if (null != cmd.ExecuteScalar())
MessageBox.Show("学生学号重复", "提示");
else
{
sql1 = "insert into student(s_number,s_name,department,profession,class";
sql2 = "values('" + txtStuNum.Text.Trim() + "','"
+ txtStuName.Text.Trim() + "','" + comBDeparture.Text.Trim() + "','" + comBprofession.Text.Trim() + "','" + comBClass.Text.Trim() + "'";
if (openFileDialog1.FileName != "")
{
//以相对路径复制照片
//string name = Path.GetFileName(openFileDialog1.FileName.ToString());
//创建与学号同名的jpg文件,保存学生照片
string sphname = Application.StartupPath.Trim() + @"\stupic\" + txtStuNum.Text.Trim() + ".jpg";
string name = @"\stupic\" + txtStuNum.Text.Trim() + ".jpg";
sql1 = sql1 + ",sphoto";
sql2 = sql2 + ",'" + name + "'";
//复制目标文件到新创建的文件
File.Copy(openFileDialog1.FileName.ToString(), sphname, true);
}
sql = sql1 + ")" + sql2 + ")";
boperate.getcom(sql);
MessageBox.Show("学生信息添加成功", "提示");
txtStuNum.Clear();
txtStuName.Clear();
}
}
else
{
//修改学生信息
string name = "";
if (openFileDialog1.FileName != "")
{
//复制目标文件
string sphname = Application.StartupPath.Trim() + @"\stupic\" + txtStuNum.Text.Trim() + ".jpg";
name = @"\stupic\" + txtStuNum.Text.Trim() + ".jpg";
File.Copy(openFileDialog1.FileName.ToString(), sphname, true);
}
sql = "update student set s_name='" + txtStuName.Text.Trim()
+ "',department='" + comBDeparture.Text.Trim() + "',profession='" + comBprofession.Text.Trim() + "',class='" + comBClass.Text.Trim() + "',sphoto='" + name + "' where s_number='" + txtStuNum.Text.Trim() + "'";
boperate.getcom(sql);
MessageBox.Show("学生信息修改成功", "提示");
}
}
}
if (cn.State == ConnectionState.Open)
cn.Close();
}
pstu.load();
}
#region 判断输入的字符串长度是否超过数据库中定义的长度
public bool Ifenogh()
{
if(boperate.IfLong(txtStuNum.Text.Trim(),10))
{
MessageBox.Show("学生学号长度超过数据库定义的10个字节!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (boperate.IfLong(txtStuName.Text.Trim(), 10))
{
MessageBox.Show("学生姓名长度超过数据库定义的10个字节!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
return true;
}
#endregion
private void comBDeparture_SelectedIndexChanged(object sender, EventArgs e)
{
if (comBDeparture.Text.Trim() != "")
{
opAndvalidate.cboxBind("select department,Profession from Profession where department = '" + comBDeparture.Text.Trim() + "'", "Profession", "Profession", comBprofession);
}
}
/// <取消>
/// 取消,并关闭对话框
/// </取消>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(*.*)|*.*|(*.gif)|*.gif|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.ico)|*.ico|(*.bmp)|*.bmp";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != "")
this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
private void comBprofession_SelectedIndexChanged(object sender, EventArgs e)
{
if (comBprofession.Text.Trim() != "")
{
opAndvalidate.cboxBind("select Profession,class from Class where Profession = '" + comBprofession.Text.Trim() + "'", "class", "class", comBClass);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -