namespace 学生信息
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
string cnStr = "Data Source=PFCYFQSI7AISRYS\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True";
SqlConnection cn = new SqlConnection(cnStr);
cn.Open();
SqlCommand cmd = new SqlCommand("delete from Student where 学号 ='" + textBox2.Text + "'", cn);
int k = cmd.ExecuteNonQuery();
if (k > 0)
{
MessageBox.Show("删除成功!");
}
cn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string cnStr = "Data Source=PFCYFQSI7AISRYS\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True";
SqlConnection cn = new SqlConnection(cnStr);
cn.Open();
SqlCommand cmd = new SqlCommand("select * from Student where( 学号='" + textBox2.Text + "')", cn);
SqlCommand cmd2 = new SqlCommand("select * from Student where( 姓名='" + textBox1.Text + "')", cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox1.Text = dr["姓名"].ToString();
textBox2.Text = dr["学号"].ToString();
textBox3.Text = dr["性别"].ToString();
textBox4.Text = dr["年龄"].ToString();
}
else
MessageBox.Show("查找的信息不存在!");
dr.Close();
cn.Close();
}
private void button4_Click(object sender, EventArgs e)
{
string cnStr = "Data Source=PFCYFQSI7AISRYS\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True";
SqlConnection cn = new SqlConnection(cnStr);
cn.Open();
SqlCommand cmd = new SqlCommand("update Student set 姓名='" + textBox1.Text + "',学号='" + textBox2.Text + "',性别='" + textBox3.Text + " ',年龄='" + textBox4.Text + " 'where 学号='" + textBox2.Text + "'", cn);
try
{
int k = cmd.ExecuteNonQuery();
if (k > 0)
{
MessageBox.Show("修改成功!");
}
}
catch (SqlException)
{
MessageBox.Show("修改失败!");
}
cn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string cnStr = "Data Source=PFCYFQSI7AISRYS\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True";
SqlConnection cn = new SqlConnection(cnStr);
cn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Student values( '" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')", cn);
int k = cmd.ExecuteNonQuery();
if (k > 0)
{
MessageBox.Show("添加成功!");
}
cn.Close();
}
}
}