You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
8.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sql
{
public partial class Form21 : Form
{
Form2 form2;
string[] str = new string[4]; //定义范围,只许多不能少
public Form21(Form2 f)//这个是带了一个参数过来的,这个是添加学生信息使用的函数
{
InitializeComponent();
button3.Visible = false; //隐藏修改按钮
form2 = f;
}
public Form21(string[] a,Form2 f) //这个是带两个参数过来的,这个是修改学生信息使用的函数
{
InitializeComponent();
for(int i=0;i<6;i++)
{
str[i] = a[i];
}
textBox1.Text = str[0];
textBox2.Text = str[1];
textBox3.Text = str[2];
textBox4.Text = str[3];//将要修改的四个值拿到这里来
textBox5.Text = str[4];
textBox6.Text = str[5];
button1.Visible = false; //隐藏保存按钮
form2 = f;
}
//添加学生信息
private void button1_Click(object sender, EventArgs e)//添加按钮的函数
{
if(textBox1.Text==""|| textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
{
MessageBox.Show("输入不完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
string sql = "Insert into Student values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
//判断是否有学号重复的,重复则禁止添加
string ID = "select * from Student";
int b=1;
Dao doo = new Dao();
IDataReader drr = doo.read(ID);
while (drr.Read())
{
string a;
a = drr["Id"].ToString();
if (a== textBox1.Text)
{
MessageBox.Show("学号为"+textBox1.Text+"的学生已经存在\n 请重新添加");
b = 0;
break;
}
}
drr.Close();//关闭连接
if (b != 0)
{
DialogResult r = MessageBox.Show(" 是否确认添加", "提示", MessageBoxButtons.OKCancel);
if (r == DialogResult.OK)
{
Dao dao = new Dao();
int i = dao.Execute(sql);
if (i > 0)
{
MessageBox.Show("添加成功");
textBox1.Text = null; //添加后归空,方便再次添加
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
textBox5.Text = null;
textBox6.Text = null;
}
form2.Table(); //实现实时刷新表
}
}
//判断是否有学号重复的,重复则禁止添加
}
}
private void button2_Click(object sender, EventArgs e)//取消按钮的函数
{
if (button1.Visible == false)
{
Close();
}
else if (textBox1.Text != "" || textBox2.Text != "" || textBox3.Text != "" || textBox4.Text != "" || textBox5.Text != "" || textBox6.Text != "")
{
DialogResult r = MessageBox.Show("退出后当前输入内容不会保存", "提示", MessageBoxButtons.OKCancel);
if (r == DialogResult.OK)
{
Close();
}
}
else
{
Close();
}
}
//修改
private void button3_Click_1(object sender, EventArgs e)//修改按钮的函数
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" )
{
MessageBox.Show("修改中有空项", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
//判断是否有学号重复的,重复则禁止添加
string ID = "select * from Student";
int b = 1;
Dao doo = new Dao();
IDataReader drr = doo.read(ID);
while (drr.Read())
{
string a;
a = drr["Id"].ToString();
if (a == textBox1.Text)
{
MessageBox.Show("学号为" + textBox1.Text + "的学生已经存在请重新修改");
b = 0;
break;
}
}
drr.Close();//关闭连接
if (b != 0)
{
if (textBox1.Text != str[0])
{
string sql = "update Student set Id='" + textBox1.Text + "'where Id='" + str[0] + "' and Name='" + str[1] + "'";
Dao dao = new Dao();
dao.Execute(sql);
str[0] = textBox1.Text;
}
if (textBox2.Text != str[1])
{
string sql = "update Student set Name='" + textBox2.Text + "' where Id='" + str[0] + "' and Name='" + str[1] + "'";
Dao dao = new Dao();
dao.Execute(sql);
str[1] = textBox2.Text;
}
if (textBox3.Text != str[2])
{
string sql = "update Student set Class='" + textBox3.Text + "' where Id='" + str[0] + "' and Name='" + str[1] + "'";
Dao dao = new Dao();
dao.Execute(sql);
str[2] = textBox3.Text;
}
if (textBox4.Text != str[3])
{
string sql = "update Student set Password='" + textBox4.Text + "' where Id='" + str[0] + "' and Name='" + str[1] + "'";
Dao dao = new Dao();
dao.Execute(sql);
str[3] = textBox4.Text;
}
if (textBox5.Text != str[4])
{
string sql = "update Student set Phone='" + textBox5.Text + "' where Id='" + str[0] + "' and Name='" + str[1] + "'";
Dao dao = new Dao();
dao.Execute(sql);
str[4] = textBox5.Text;
}
if (textBox6.Text != str[5])
{
string sql = "update Student set Emali='" + textBox6.Text + "' where Id='" + str[0] + "' and Name='" + str[1] + "'";
Dao dao = new Dao();
dao.Execute(sql);
str[5] = textBox6.Text;
}
DialogResult r = MessageBox.Show("是否确认修改", "提示", MessageBoxButtons.OKCancel);
if (r == DialogResult.OK)
{
form2.Table(); //实现实时刷新表
Close();
}
}
//判断是否有学号重复的,重复则禁止添加
}
}
private void button4_Click(object sender, EventArgs e)//后面的是几个x的图标用处是将文本框里的东西置空
{
textBox1.Text = null;
}
private void button5_Click(object sender, EventArgs e)
{
textBox2.Text = null;
}
private void button6_Click(object sender, EventArgs e)
{
textBox3.Text = null;
}
private void button7_Click(object sender, EventArgs e)
{
textBox4.Text = null;
}
private void label5_Click(object sender, EventArgs e)
{
}
}
}