根据课设要求
准备工作:
microsoft sql server management studio
首先在数据库中建立EDUC数据库,并建立学生信息表
并添加sa用户名和密码
VS2015
1.新建项目
模板选Visual c#->Windows窗体应用程序
2.在窗口新建一个dataGridView1,用于显示学生信息(右击属性添加列,编辑列以达到效果,注意要一一对应)
3.在工具箱里添加几个button,并双击button得到对应的消息处理函数
4.再添加几个label和textbox
5.学生信息显示代码(连接数据库)
private void button1_Click(object sender, EventArgs e)//显示
{
string str, selectsql;
str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******"; //SQL Server链接字符串
selectsql = "select * from Stu";
SqlConnection mycon = new SqlConnection(str);//Sql链接类的实例化
mycon.Open(); //打开数据库
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str); //创建DataAdapter数据适配器实例
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");//创建DataSet实例 myda.Fill(myds,"自定义虚拟表名");使用DataAdapter的Fill方法(填充),调用SELECT命令
dataGridView1.DataSource = myds.Tables["Stu"];//虚拟表Stu
}
6.刷新文本代码
private void button4_Click(object sender, EventArgs e)
{
textBox6.Text = String.Empty;
textBox7.Text = String.Empty;
textBox9.Text = String.Empty;
textBox10.Text = String.Empty;
textBox11.Text = String.Empty;
textBox12.Text = String.Empty;
}
7.添加学生信息代码(学号重复学生不得添加)
private void button5_Click(object sender, EventArgs e)//添加
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
if(textBox6.Text=="")
{
MessageBox.Show("学号为空,添加失败!");
return;
}
else if(TheSame()==0)
{
MessageBox.Show("学号重复添加失败");
return;
}
else
{
string selectsql = "insert into Stu values('" + textBox6.Text + "','" + textBox7.Text + "','" + textBox11.Text + "','" + textBox10.Text + "','" + textBox12.Text + "','" + textBox9.Text + "')";
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
SqlDataReader sdr;
sdr = cmd.ExecuteReader();
conn.Close();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
MessageBox.Show("添加成功"); } } public int TheSame() { string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=hqq1030"; SqlConnection conn = new SqlConnection(str); conn.Open(); string selectsql = "select sno from Stu where sno='" + textBox6.Text + "'"; SqlCommand cmd = new SqlCommand(selectsql, conn); SqlDataReader sdr; sdr = cmd.ExecuteReader(); if(sdr.HasRows) { return 0; } else { return 1; } }
8.修改学生信息代码(选择学生信息到textbook里进行修改,再update到学生信息表中,其中textBox_TextChanged可双击textBox得到,只要改变了textBox里的值就会响应)
private void button6_Click(object sender, EventArgs e)//更新
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=********";
SqlConnection conn = new SqlConnection(str);
conn.Open();
if (dataGridView1.SelectedRows.Count != 1) return;
if (dataGridView1.CurrentRow == null) return;
DataRowView row = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
if (row["sno"] == null) return;//可以进行快速监视
string s1 = Convert.ToString(row["sno"]);
string s2 = Convert.ToString(row["sname"]);
string s3 = Convert.ToString(row["sex"]);
string s4 = Convert.ToString((row["birthday"]));
string s5 = Convert.ToString(row["Sdept"]);
string s6 = Convert.ToString(row["home"]);
textBox6.Text = s1;
textBox7.Text = s2;
textBox11.Text = s3;
textBox12.Text = s4;
textBox9.Text = s5;
textBox10.Text = s6;
textBox7.TextChanged += textBox7_TextChanged;
textBox11.TextChanged += textBox11_TextChanged;
textBox12.TextChanged += textBox12_TextChanged;
textBox9.TextChanged += textBox9_TextChanged;
textBox10.TextChanged += textBox10_TextChanged;
MessageBox.Show("更新成功!");
}
private void textBox7_TextChanged(object sender, EventArgs e)
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "update Stu set sname= '" + textBox7.Text + "' where sno = '" + textBox6.Text + "'";
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
private void textBox11_TextChanged(object sender, EventArgs e)
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "update Stu set sex ='" + textBox11.Text + "' where sno = '" + textBox6.Text + "'";
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
private void textBox12_TextChanged(object sender, EventArgs e)
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=********";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "update Stu set birthday= '" + textBox12.Text + "' where sno = '" + textBox6.Text + "'";
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "update Stu set Sdept= '" + textBox9.Text + "' where sno = '" + textBox6.Text + "'";
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "update Stu set home= '" + textBox10.Text + "' where sno = '" + textBox6.Text + "'";
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
9.删除学生信息代码(鼠标点选相应学生删除)
private void button7_Click(object sender, EventArgs e)//删除
{
string selectsql;
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
if(textBox6.Text=="")
{
selectsql = "delete from Stu where sno ='" + textBox6.Text + "'";
}
else
{
if (dataGridView1.SelectedRows.Count != 1) return;
if (dataGridView1.CurrentRow == null) return;
DataRowView row = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
if (row["sno"] == null) return;//可以进行快速监视
string bd = Convert.ToString(row["sno"]);
selectsql = "delete from Stu where sno = " + bd + "";
}
SqlCommand cmd = new SqlCommand(selectsql, conn);
cmd.CommandType = CommandType.Text;
//ExecuteNonQuery()就是给SqlDataReader对象一个可以访问查询到的结果的渠道
int ret = cmd.ExecuteNonQuery();//受影响的行数(总数)
if (ret == -1)
{
MessageBox.Show("删除失败!");
return;
}
else
{
MessageBox.Show("删除成功!");
}
conn.Close();
}
10.查询学生信息代码
private void button2_Click(object sender, EventArgs e)//查询
{
string str = "Data Source=LAPTOP-6H740O0C;Initial Catalog=EDUC;uid=sa;pwd=*******";
SqlConnection conn = new SqlConnection(str);
conn.Open();
if (checkBox1.Checked == true)
{
string selectsql = "select * from Stu where sno ='" + textBox1.Text + "'";
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str);
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");
dataGridView1.DataSource = myds.Tables["Stu"];
}
if (checkBox2.Checked == true)
{
string selectsql = "select * from Stu where sname ='" + textBox2.Text + "'";
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str);
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");
dataGridView1.DataSource = myds.Tables["Stu"];
}
if (checkBox3.Checked == true)
{
string selectsql;
if (checkBox1.Checked == true)
{
selectsql = "select * from Stu where sno ='" + textBox1.Text + "'and sex='" + textBox3.Text + "'";
}
else if (checkBox2.Checked == true)
{
selectsql = "select * from Stu where sname ='" + textBox2.Text + "'and sex='" + textBox3.Text + "'";
}
else if (checkBox4.Checked == true)
{
if (checkBox5.Checked == true)
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'and Sdept='" + textBox5.Text + "'";
}
else
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'";
}
}
else if (checkBox5.Checked == true)
{
if (checkBox4.Checked == true)
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'and Sdept='" + textBox5.Text + "'";
}
else
{
selectsql = "select * from Stu where Sdept='" + textBox5.Text + "'and sex='" + textBox3.Text + "'";
}
}
else
{
selectsql = "select * from Stu where sex ='" + textBox3.Text + "'";
}
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str);
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");
dataGridView1.DataSource = myds.Tables["Stu"];
}
if (checkBox4.Checked == true)
{
string selectsql;
if (checkBox1.Checked == true)
{
selectsql = "select * from Stu where sno ='" + textBox1.Text + "'and birthday='" + textBox4.Text + "'";
}
else if (checkBox2.Checked == true)
{
selectsql = "select * from Stu where sname ='" + textBox2.Text + "'and birthday='" + textBox4.Text + "'";
}
else if (checkBox3.Checked == true)
{
if (checkBox5.Checked == true)
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'and Sdept='" + textBox5.Text + "'";
}
else
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'";
}
}
else if (checkBox5.Checked == true)
{
if (checkBox3.Checked == true)
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'and Sdept='" + textBox5.Text + "'";
}
else
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and Sdept='" + textBox5.Text + "'";
}
}
else
{
selectsql = "select * from Stu where birthday ='" + textBox4.Text + "'";
}
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str);
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");
dataGridView1.DataSource = myds.Tables["Stu"];
}
if (checkBox5.Checked == true)
{
string selectsql;
if (checkBox1.Checked == true)
{
selectsql = "select * from Stu where sno ='" + textBox1.Text + "'and Sdept='" + textBox5.Text + "'";
}
else if (checkBox2.Checked == true)
{
selectsql = "select * from Stu where sname ='" + textBox2.Text + "'and Sdept='" + textBox5.Text + "'";
}
else if (checkBox4.Checked == true)
{
if (checkBox3.Checked == true)
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'and Sdept='" + textBox5.Text + "'";
}
else
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and Sdept='" + textBox5.Text + "'";
}
}
else if (checkBox3.Checked == true)
{
if (checkBox4.Checked == true)
{
selectsql = "select * from Stu where birthday='" + textBox4.Text + "'and sex='" + textBox3.Text + "'and Sdept='" + textBox5.Text + "'";
}
else
{
selectsql = "select * from Stu where Sdept='" + textBox5.Text + "'and sex='" + textBox3.Text + "'";
}
}
else
{
selectsql = "select * from Stu where Sdept ='" + textBox5.Text + "'";
}
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str);
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");
dataGridView1.DataSource = myds.Tables["Stu"];
}
if (checkBox6.Checked == true)//模糊查询
{
string selectsql;
if (checkBox2.Checked==true)
{
selectsql = "select * from Stu where sname like'" + textBox2.Text + "%'";
}
else if (checkBox1.Checked == true)
{
selectsql = "select * from Stu where sno like'" + textBox1.Text + "%'";
}
else if (checkBox4.Checked == true)
{
selectsql = "select * from Stu where birthday like'" + textBox4.Text + "%'";
}
else
{
selectsql = "select * from Stu where sdept like'" + textBox5.Text + "%'";
}
SqlDataAdapter myda = new SqlDataAdapter(selectsql, str);
DataSet myds = new DataSet();
myda.Fill(myds, "Stu");
dataGridView1.DataSource = myds.Tables["Stu"];
}
}
最后总结一下,textBox_TextChanged并不是最好的方法,其实可以直接修改dataGridView1的,也是双击得到对应的函数。
还有就是查询写的挺麻烦的,如果有更好的写法还望指正