1.
学生另一个管理页面:
政治面貌Label控件后是ComboBox控件,点击右上角的小三角,往里面编辑项
label年龄控件后是一个 NumericUpDown控件可以进行加减年龄
兴趣与爱好是一个GroupBox控件,Text属性改为兴趣与爱好,里面放5个CheckBox控件,Text属性改为响应的名字
双击按钮确定,里面写上代码:
private void btnOk_Click(object sender, EventArgs e)
{
string no = txtStuNo.Text;
string name = txtName.Text;
string address = txtJG.Text;
string sex;
if (radbtnMan.Checked == true)
{
sex = radbtnMan.Text;
}
else
{
sex = radbtnWoman.Text;
}
string zhengzhi = cboZZ.Text;
string aihao = "";
if (cekRead.Checked== true)
{
aihao = cekRead.Text;
}
if (cekSport.Checked == true)
{
aihao += cekSport.Text;
}
if (cekTravel.Checked == true)
{
aihao += cekTravel.Text;
}
if (cekGame.Checked == true)
{
aihao += cekGame.Text;
}
if (cekAnother.Checked == true)
{
aihao += cekAnother.Text;
}
MessageBox.Show(no + name + address + sex + zhengzhi + aihao);
}
功能是提示填写的信息:
点击确定:
双击退出代码:
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
页面加载男默认选中,双击页面外围,里面填上代码:
private void frmInformation_Load(object sender, EventArgs e)
{
radbtnMan.Checked= true;
}
2.
编辑班级页面:
添加一个DataGridView数据控件,点击右上角小三角,进行连接数据库中的表:
双击更新,里面的代码:
private void btnNew_Click(object sender, EventArgs e)
{
try
{
DBhelp.conn.Open();
string sqlstr = string.Format("update tb_Class set ClassName='{0}' where ClassID='{1}'", txtClassName.Text, txtClassID.Text);
DBhelp.comm.CommandText = sqlstr;
DBhelp.comm.Connection = DBhelp.conn;
if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("更新成功");
}
else
{
MessageBox.Show("更新失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
把1改为2
双击删除,里面的代码:
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
DBhelp.conn.Open();
string sqlstr = string.Format("delete from tb_Class where ClassID='{0}'", txtClassID.Text);
DBhelp.comm.CommandText = sqlstr;
DBhelp.comm.Connection = DBhelp.conn;
if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
双击关闭,代码:
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
当点击一个数据在下方输入框内显示,需要给DataGridView控件的CellClick属性添加点击事件,双击那个属性:
代码如下:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtClassID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtClassName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
学生信息编辑页面:
2个GroupBox组控件里面包含其他的控件,如果不想显示组控件的名字,把Text属性内容删除掉
双加查询,里面代码:
private void btnSelect_Click(object sender, EventArgs e)
{
string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
SqlConnection a = new SqlConnection(conn);
try
{
a.Open();
string sqlstr = string.Format("select * from tb_Student where StudentID='{0}'", txtStudentNo.Text);
SqlCommand comm = new SqlCommand();
comm.Connection = a;
comm.CommandText = sqlstr;
SqlDataReader read = comm.ExecuteReader();
if (read.Read())
{
txtStudentID.Text = read[0].ToString();
txtStudentName.Text = read[1].ToString();
txtStudentGender.Text = read[2].ToString();
txtBirthday.Text = read[3].ToString();
txtClassID.Text = read[4].ToString();
txtMobilePhone.Text = read[5].ToString();
txtAddress.Text = read[6].ToString();
}
else
{
MessageBox.Show("学号不存在!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
a.Close();
}
}
双击修改,里面代码:
private void btnAlter_Click(object sender, EventArgs e)
{
try
{
DBhelp.conn.Open();
string sqlstr = string.Format("update tb_Student set StudentName='{0}',Birthday='{1}',ClassID='{2}',MobilePhone='{3}',Gender='{4}',Address='{5}' where StudentID='{6}'", txtStudentName.Text, txtBirthday.Text, txtClassID.Text, txtMobilePhone.Text, txtStudentGender.Text, txtAddress.Text, txtStudentNo.Text);
DBhelp.comm.CommandText = sqlstr;
DBhelp.comm.Connection = DBhelp.conn;
if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
修改电话:
双击删除,里面代码:
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
DBhelp.conn.Open();
string sqlstr = string.Format("delete from tb_Student where StudentID='{0}'", txtStudentNo.Text);
DBhelp.comm.CommandText = sqlstr;
DBhelp.comm.Connection = DBhelp.conn;
if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
双击关闭,里面代码:
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
3.
用户修改密码页面:
双击确认修改,里面代码:
private void btnOK_Click(object sender, EventArgs e)
{
try
{
DBhelp.conn.Open();
string sqlstr = string.Format("update tb_User set UserName='{0}',UserPasswd='{1}' where UserID='{2}'", txtUserName.Text,txtUserPasswd.Text, txtUserID.Text);
DBhelp.comm.CommandText = sqlstr;
DBhelp.comm.Connection = DBhelp.conn;
if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBhelp.conn.Close();
}
}
4.
主页面:
首先添加一个菜单栏控件MenuStrip:添加各项
再添加一个工具栏控件:ToolStrip:添加各项,
如果选择button,向显示字体则设置:
状态栏控件:StatusStrip,可选择以下各项:
在添加一个TreeView控件,显示层次信息
点击右上方小三角,编辑节点
再添加一个DataGridView控件:显示数据库中的表:
点击右上角小三角: