📄 insertstudent.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class insertstudent : Form
{
public string USERID;
private DataSet ds = new DataSet();
private SqlDataAdapter da = new SqlDataAdapter();
public insertstudent()
{
InitializeComponent();
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox3.Text != "" && textBox2.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "")
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "select * from 学生信息";
mycommand.Connection = myconnection;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = mycommand;
da.SelectCommand.Connection = myconnection;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "学生信息");
DataTable dt = ds.Tables["学生信息"];
DataRow dr = dt.NewRow();
dr["学号"] = Convert.ToInt64(textBox1.Text);
dr["姓名"] = textBox2.Text;
dr["专业"] = textBox3.Text;
if (radioButton3.Checked)
{
dr["性别"] = "男";
}
else if (radioButton4.Checked)
{
dr["性别"] = "女";
}
dr["年龄"] = Convert.ToInt32(textBox5.Text);
dr["电话"] = Convert.ToInt64(textBox6.Text);
dr["地址"] = textBox7.Text;
ds.Tables["学生信息"].Rows.Add(dr);
da.Update(ds, "学生信息");
MessageBox.Show("添加成功!");
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";
textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = "";
myconnection.Close();
}
catch (OverflowException fe)
{
MessageBox.Show("学号或电话号码太大,请重新输入!"+fe.Message);
}
catch (FormatException fe)
{
MessageBox.Show("学号,年龄或电话格式不正确," + fe.Message);
}
catch (SqlException se)
{
MessageBox.Show("添加时出现错误!" + se.Message);
}
}
else
{
MessageBox.Show("请把各项的数据输入完!");
}
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
dataGridView1.DataSource = null;
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "select * from 学生信息 where 学号='" + Convert.ToInt64(textBox4.Text.Trim()) + "'";
mycommand.Connection = myconnection;
da.SelectCommand = mycommand;
da.SelectCommand.Connection = myconnection;
da.Fill(ds, "学生信息");
if (ds.Tables["学生信息"].Rows.Count < 1)
{
MessageBox.Show("不存在该学生!");
}
else
{
dataGridView1.DataSource = ds.Tables["学生信息"];
button4.Enabled = true;
button2.Enabled = false;
}
textBox4.Text = "";
}
catch (OverflowException fe)
{
MessageBox.Show("电话号码太大,请重新输入!"+fe.Message);
}
catch (FormatException fe)
{
MessageBox.Show("学号的格式不正确," + fe.Message);
}
}
else
{
MessageBox.Show("请输入需要修改的学生的学号!");
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "学生信息");
MessageBox.Show("更新成功!");
ds.Clear();
button2.Enabled = true;
button4.Enabled = false;
}
catch (SqlException ie)
{
MessageBox.Show("请先修改需要更改的数据!" + ie.Message);
ds.Clear();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand1 = new SqlCommand();
SqlCommand mycommand2 = new SqlCommand();
mycommand1.CommandText = "delete from 学生信息 where 学号='" + Convert.ToInt64(textBox4.Text.Trim()) + "' ";
mycommand2.CommandText = "delete from 成绩表 where 学号='" + Convert.ToInt64(textBox4.Text.Trim()) + "' ";
mycommand1.Connection = myconnection;
mycommand2.Connection = myconnection;
int result2 = mycommand2.ExecuteNonQuery();
int result1 = mycommand1.ExecuteNonQuery();
if (result1 > 0 || result2>0)
MessageBox.Show("删除成功");
else
MessageBox.Show("该学生不存在");
myconnection.Close();
textBox4.Text = "";
}
catch (FormatException fe)
{
MessageBox.Show("学号格式错误," + fe.Message);
}
catch (SqlException se)
{
MessageBox.Show("删除出现错误,请将该生的成绩先删除," + se.Message);
}
}
else
{
MessageBox.Show("请输入需要删除的学生的学号!");
}
}
private void button3_Click(object sender, EventArgs e)
{
textBox4.Text = "";
}
private void insertstudent_Load(object sender, EventArgs e)
{
button4.Enabled = false ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -