上来先”一键“三表
增加
删除
更改
查找
private void button2_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox5.Text.Trim();
String StuAge = textBox4.Text.Trim(); //读取需要插入的值
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=LaoZhao;User ID=sa;Password=6632174");
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept) " + //拼接字符串
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuSdept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter2.Fill(this.laoZhaoDataSet1.Student);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void chajian_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox5.Text.Trim();
String StuAge = textBox4.Text.Trim();
String conn = "Data Source=.;Initial Catalog=LaoZhao;User ID=sa;Password=6632174";
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='"+StuID+"'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
sqlConnection.Close();
}
}
private void shanjian_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=LaoZhao;User ID=sa;Password=6632174"); //定义
try
{
con.Open(); //打开
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con); //使用
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose(); //释放
}
this.studentTableAdapter2.Fill(this.laoZhaoDataSet1.Student);
}
private void gaijian_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox5.Text.Trim();
String StuAge = textBox4.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=LaoZhao;User ID=sa;Password=6632174");
try
{
string insertStr = "";
string insertStr1 = "";
string insertStr2 = "";
string insertStr3 = "";
con.Open();
if (StuName != "")
{
insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'"; //修改名字
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if (StuSex != "")
{
insertStr1 = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'"; //修改性别
SqlCommand cmd1 = new SqlCommand(insertStr1, con);
cmd1.ExecuteNonQuery();
}
if (StuSdept != "")
{
insertStr2 = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'"; //修改专业
SqlCommand cmd2 = new SqlCommand(insertStr2, con);
cmd2.ExecuteNonQuery();
}
if (StuAge != "")
{
insertStr3 = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'"; //修改年龄
SqlCommand cmd3 = new SqlCommand(insertStr3, con);
cmd3.ExecuteNonQuery();
}
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.studentTableAdapter2.Fill(this.laoZhaoDataSet1.Student);
}
}
}