using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentManageSys
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
///
/// 退出按钮单击事件
///
/// 事件源
/// 事件参数
private void tsmiExit_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show(“确定要退出吗?”,“提示”,MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
Application.Exit();
}
else
{
}
}
///
/// 查询学生信息单击事件
///
///
///
private void tsmiSearch_Click(object sender, EventArgs e)
{
FrmSearch fs = new FrmSearch();
fs.MdiParent = this;
fs.Show();
}
///
/// 添加学生信息单击事件
///
/// 事件源
/// 事件参数
private void tsmiAdd_Click(object sender, EventArgs e)
{
FrmAdd fa = new FrmAdd();
fa.MdiParent = this;
fa.Show();
}
}
}
**
**
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace StudentManageSys
{
public partial class FrmAdd : Form
{
DBhelper db = new DBhelper();
DataSet ds = new DataSet();
SqlDataAdapter adapter;
public FrmAdd()
{
InitializeComponent();
}
///
/// 保存按钮单击事件
///
///
///
private void btnSave_Click(object sender, EventArgs e)
{
//非空验证
if (CheckInput())
{
if (AddInfo())
{
MessageBox.Show(“添加成功”);
}
else
{
MessageBox.Show(“添加失败”);
}
}
else
{
MessageBox.Show(“请填写完整信息”);
}
//绑定CBO
BindCbo();
}
public void BindCbo()
{
StringBuilder sql = new StringBuilder();
sql.Append(“select * from Grade”);
adapter = db.GetAdapter(sql.ToString());
adapter.Fill(ds, “Grade”);
//添加一行
DataRow row = ds.Tables[“Grade”].NewRow();
row[0] = “-1”;
row[1] = “全部”;
ds.Tables[“Grade”].Rows.InsertAt(row, 0);
this.cboGrade.DataSource = ds.Tables["Grade"];
this.cboGrade.ValueMember = "GradeId";
this.cboGrade.DisplayMember = "GradeName";
}
///
/// 非空验证
///
///
public bool CheckInput()
{
if (this.txtPwd.Text.Trim().Equals(String.Empty)
|| this.cboGrade.Text.Trim().Equals("全部")
|| this.txtName.Text.Trim().Equals(String.Empty)
|| this.txtPhone.Text.Trim().Equals(String.Empty))
{
return false;
}
else
{
return true;
}
}
///
/// 添加信息
///
///
public bool AddInfo()
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat(@"INSERT INTO [dbo].[Student]
([LoginPwd]
,[StudentName]
,[Gender]
,[GradeId]
,[Phone]
,[Address]
,[Birthday]
,[Email])
VALUES
('{0}'
, '{1}'
, '{2}'
,{ 3}
,'{4}'
,'{5}'
,'{6}'
'{7}')",
this.txtPwd.Text.Trim(),
this.txtName.Text.Trim(),
this.rdoMale.Checked == true,
this.cboGrade.SelectedValue,
this.txtPhone.Text.Trim(),
this.txtAddress.Text.Trim(),
this.txtBirthday.Text.Trim(),
this.txtEmail.Text.Trim());
int result = db.ExecuteNonQuery(sql.ToString());
if (result >0)
{
return true;
}
else
{
return false;
}
}
///
/// 清空按钮
///
/// 事件源
/// 事件参数
private void btnClear_Click(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
if (control is TextBox)
{
control.Text = "";
}
}
this.cboGrade.SelectedIndex = 0;
this.rdoMale.Checked = true;
}
///
/// 关闭按钮单击事件
///
/// 事件源
/// 事件参数
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void FrmAdd_Load(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace StudentManageSys
{
public partial class FrmSearch : Form
{
//数据库帮助类
DBhelper db = new DBhelper();
//数据集
DataSet ds = new DataSet();
//适配器
SqlDataAdapter adapter;
public FrmSearch()
{
InitializeComponent();
}
///
/// 窗体加载事件
///
/// 事件源
/// 事件参数
private void FrmSearch_Load(object sender, EventArgs e)
{
//绑定CBO
BindCbo();
//清除多余列
this.dgvShow.AutoGenerateColumns = false;
//绑定Dgv
BindDgv();
//设置性别组合框默认选中第一项
this.cboGender.SelectedIndex = 0;
}
///
/// 绑定CBO
///
public void BindCbo()
{
StringBuilder sql = new StringBuilder();
sql.Append("select * from Grade");
adapter = db.GetAdapter(sql.ToString());
adapter.Fill(ds, "Grade");
//添加一行
DataRow row = ds.Tables["Grade"].NewRow();
row[0] = "-1";
row[1] = "全部";
ds.Tables["Grade"].Rows.InsertAt(row,0);
this.cboGrade.DataSource = ds.Tables["Grade"];
this.cboGrade.ValueMember = "GradeId";
this.cboGrade.DisplayMember = "GradeName";
}
///
/// 绑定Dgv
///
public void BindDgv()
{
if (ds.Tables["StudentAndGrade"] != null)
{
ds.Tables["StudentAndGrade"].Clear();
}
StringBuilder sql = new StringBuilder();
sql.Append(@"select * from Grade,Student
where Grade.GradeId = Student.GradeId");
adapter = db.GetAdapter(sql.ToString());
adapter.Fill(ds, "StudentAndGrade");
this.dgvShow.DataSource = ds.Tables["StudentAndGrade"];
}
///
/// 查询按钮单击事件
///
/// 事件源
/// 事件参数
private void btnSearch_Click(object sender, EventArgs e)
{
DataView dv = new DataView(ds.Tables["StudentAndGrade"]);
if (!this.txtName.Text.Trim().Equals(String.Empty)&&
this.cboGrade.Text.Trim().Equals("全部")
&& this.cboGender.Text.Trim().Equals("全部"))
{
//1
dv.RowFilter = string.Format("StudentName like '%{0}%'",
this.txtName.Text.Trim());
}
else if(this.txtName.Text.Trim().Equals(String.Empty) &&
!this.cboGrade.Text.Trim().Equals("全部")&&
this.cboGender.Text.Trim().Equals("全部"))
{
//2
dv.RowFilter = string.Format("GradeId = {0}",
this.cboGrade.SelectedValue);
}
else if (this.txtName.Text.Trim().Equals(String.Empty) &&
this.cboGrade.Text.Trim().Equals("全部")&&
!this.cboGender.Text.Trim().Equals("全部"))
{
//3
dv.RowFilter = string.Format("Gender = '{0}'",
this.cboGender.Text.Trim().Equals("男")?1:0);
}
else if (!this.txtName.Text.Trim().Equals(String.Empty) &&
!this.cboGrade.Text.Trim().Equals("全部")&&
this.cboGender.Text.Trim().Equals("全部"))
{
//12
dv.RowFilter = string.Format("StudentName like '%{0}%' and GradeId = {1}",
this.txtName.Text.Trim(), this.cboGrade.SelectedValue);
}
else if (!this.txtName.Text.Trim().Equals(String.Empty) &&
this.cboGrade.Text.Trim().Equals("全部")&&
!this.cboGender.Text.Trim().Equals("全部"))
{
//13
dv.RowFilter = string.Format("StudentName like '%{0}%' and Gender = '{1}'",
this.txtName.Text.Trim(), this.cboGender.Text.Trim().Equals("男") ? 1 : 0);
}
else if (this.txtName.Text.Trim().Equals(String.Empty) &&
!this.cboGrade.Text.Trim().Equals("全部")&&
!this.cboGender.Text.Trim().Equals("全部"))
{
//23
dv.RowFilter = string.Format("GradeId = {0} and Gender = '{1}'",
this.cboGrade.SelectedValue, this.cboGender.Text.Trim().Equals("男") ? 1 : 0);
}
else if (this.txtName.Text.Trim().Equals(String.Empty) &&
this.cboGrade.Text.Trim().Equals("全部")
&& this.cboGender.Text.Trim().Equals("全部"))
{
//123
dv.RowFilter = string.Format("StudentName like '%{0}%' and GradeId = {1} and Gender = '{2}'",
this.txtName.Text.Trim(),this.cboGrade.SelectedValue, this.cboGender.Text.Trim().Equals("男") ? 1 : 0);
}
this.dgvShow.DataSource = dv;
}
///
/// 删除单击事件
///
/// 事件源
/// 事件参数
private void tsmiDelete_Click(object sender, EventArgs e)
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat(@"DELETE FROM [dbo].[Student]
WHERE StudentNo = { 0}",
this.dgvShow.SelectedRows[0].Cells[0].Value.ToString());
int result = db.ExecuteNonQuery(sql.ToString());
if (result > 0)
{
MessageBox.Show("删除成功");
BindDgv();
}
else
{
MessageBox.Show("删除失败");
}
}
///
/// 修改窗体
///
///
///
private void tsmiUpdate_Click(object sender, EventArgs e)
{
FrmUpdate fu = new FrmUpdate();
//传值
fu.StudentNo = this.dgvShow.SelectedRows[0].Cells[0].Value.ToString();
//指定修改窗体可以使用本窗体方法
fu.fs = this;
fu.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace StudentManageSys
{
public partial class FrmUpdate : Form
{
//查询窗体(定时刷新)
public FrmSearch fs;
DBhelper db = new DBhelper();
DataSet ds = new DataSet();
SqlDataAdapter adapter;
//传值变量
public string StudentNo;
public FrmUpdate()
{
InitializeComponent();
}
///
/// 关闭按钮单击事件
///
/// 事件源
/// 事件参数
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 清空按钮单击事件
///
/// 事件源
/// 事件参数
private void btnClear_Click(object sender, EventArgs e)
{
this.txtAddress.Text.Trim();
this.txtBirthday.Text.Trim();
this.txtEmail.Text.Trim();
this.txtName.Text.Trim();
this.txtPhone.Text.Trim();
this.txtPwd.Text.Trim();
this.cboGrade.SelectedIndex = 0;
this.rdoMale.Checked = true;
}
///
/// 保存按钮单击事件
///
///
///
private void btnSave_Click(object sender, EventArgs e)
{
//非空验证
if (CheckInput())
{
//修改
if (UpdateInfo())
{
MessageBox.Show("修改成功");
fs.BindDgv();//刷新
this.Close();
}
else
{
MessageBox.Show("修改失败");
}
}
else
{
MessageBox.Show("请填写完整信息");
}
//绑定CBO
BindCbo();
}
public void BindCbo()
{
StringBuilder sql = new StringBuilder();
sql.Append("select * from Grade");
adapter = db.GetAdapter(sql.ToString());
adapter.Fill(ds, "Grade");
//添加一行
DataRow row = ds.Tables["Grade"].NewRow();
row[0] = "-1";
row[1] = "全部";
ds.Tables["Grade"].Rows.InsertAt(row, 0);
this.cboGrade.DataSource = ds.Tables["Grade"];
this.cboGrade.ValueMember = "GradeId";
this.cboGrade.DisplayMember = "GradeName";
}
///
/// 非空验证
///
///
public bool CheckInput()
{
if (this.txtPwd.Text.Trim().Equals(String.Empty)
|| this.cboGrade.Text.Trim().Equals("全部")
|| this.txtName.Text.Trim().Equals(String.Empty)
|| this.txtPhone.Text.Trim().Equals(String.Empty))
{
return false;
}
else
{
return true;
}
}
///
/// 添加信息
///
///
public bool UpdateInfo()
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat(@"UPDATE [dbo].[Student]
SET [LoginPwd] = '{0}'
,[StudentName] = '{1}'
,[Gender] = '{2}'
,[GradeId] = {3}
,[Phone] = '{4}'
,[Address] = '{5}'
,[Birthday] = '{6}'
,[Email] = '{7}'
WHERE StudentNo = {8})",
this.txtPwd.Text.Trim(),
this.txtName.Text.Trim(),
this.rdoMale.Checked == true,
this.cboGrade.SelectedValue,
this.txtPhone.Text.Trim(),
this.txtAddress.Text.Trim(),
this.txtBirthday.Text.Trim(),
this.txtEmail.Text.Trim(),
StudentNo);
int result = db.ExecuteNonQuery(sql.ToString());
if (result > 0)
{
return true;
}
else
{
return false;
}
}
///
/// 窗体加载事件
///
///
///
private void FrmUpdate_Load(object sender, EventArgs e)
{
BindCbo();
this.txtStudentNo.Text = StudentNo;
GetInfoByStudentNo();
}
///
/// 通过学号查询学生信息
///
public void GetInfoByStudentNo()
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat(@"select * from Student
where StudentNo = {0}",
StudentNo);
SqlDataReader reader = db.ExecuteReader(sql.ToString());
while (reader.Read())
{
string Loginpwd = reader[1].ToString();
string StudentName = reader[2].ToString();
string Gender = reader[3].ToString();
string GradeId = reader[4].ToString();
string Phone = reader[5].ToString();
string Address = reader[6].ToString();
string Birthday = reader[7].ToString();
string Email = reader[8].ToString();
this.txtPwd.Text = Loginpwd;
this.txtName.Text = StudentName;
//性别单选按钮
if (Gender.Equals("1"))
{
this.rdoMale.Checked = true;
}
else
{
this.rdoMale.Checked = false;
}
//年级框
if (GradeId.Equals("1"))
{
this.cboGrade.SelectedIndex = 1;
}
else if (GradeId.Equals("2"))
{
this.cboGrade.SelectedIndex = 2;
}else if (GradeId.Equals("3"))
{
this.cboGrade.SelectedIndex = 3;
}
this.txtPhone.Text = Phone;
this.txtAddress.Text = Address;
this.txtBirthday.Text = Birthday;
this.txtEmail.Text = Email;
}
reader.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace StudentManageSys
{
class DBhelper
{
private string connString = “Data Source=.;Initial Catalog=MySchool;Integrated Security=True”;
private SqlConnection conn;
public SqlConnection Conn
{
get
{
if (this.conn == null)
{
conn = new SqlConnection(connString);
}
return conn;
}
}
///
/// 打开数据库
///
public void OpenConnection()
{
if (this.Conn.State == ConnectionState.Broken)
{
this.Conn.Close();
this.Conn.Open();
}
if (this.Conn.State == ConnectionState.Closed)
{
this.Conn.Open();
}
}
///
/// 关闭数据库
///
public void CloseConnection()
{
if (this.Conn.State == ConnectionState.Broken||
this.Conn.State == ConnectionState.Open)
{
this.Conn.Close();
}
}
///
/// 增删改
///
/// SQL语句
/// 受影响行数
public int ExecuteNonQuery(string sql)
{
int result = 0;
try
{
this.OpenConnection();
SqlCommand comm = new SqlCommand(sql, Conn);
result = comm.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return -1;
}
finally
{
this.CloseConnection();
}
return result;
}
///
/// 查询单行单列
///
/// SQL语句
/// 受影响行数
public object ExecuteScalar(string sql)
{
object result = null;
try
{
this.OpenConnection();
SqlCommand comm = new SqlCommand(sql, Conn);
result = comm.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
this.CloseConnection();
}
return result;
}
///
/// 查询多行多列
///
/// SQL语句
/// 受影响行数
public SqlDataReader ExecuteReader(string sql)
{
SqlDataReader reader = null;
try
{
this.OpenConnection();
SqlCommand comm = new SqlCommand(sql, Conn);
reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
return reader;
}
///
/// 获取适配器对象
///
/// SQL语句
/// 适配器对象
public SqlDataAdapter GetAdapter(string sql)
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(sql, Conn);
return adapter;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
}
}