//数据访问层using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MySchool.DAL
{
public class GradeidDal
{
public DataTable allGradeID()
{
string str = sqlHelper.str;
SqlConnection con=new SqlConnection(str);
string sql = "select gradeid,gradename from grade";
SqlDataAdapter sqa=new SqlDataAdapter(sql,con);
DataSet ds=new DataSet();
sqa.Fill(ds, "GradeInfo");
return ds.Tables["GradeInfo"];
}
public int Gradeidall(string Name)
{
string str = sqlHelper.str;
SqlConnection con = new SqlConnection(str);
string sql = "select gradeid from grade where GradeName='"+Name+"'";
SqlCommand com = new SqlCommand(sql,con);
con.Open();
int Gradeid = Convert.ToInt32(com.ExecuteScalar());
con.Close();
return Gradeid;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace MySchool.DAL
{
public static class sqlHelper
{
//用静态的方法调用的时候不用创建SQLHelper的实例
//Execetenonquery
// public static string Constr = "server=HAPPYPIG\\SQLMODEL;database=shooltest;uid=sa;pwd=6375196;";
public static string str = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
public static int id;
/// <summary>
/// 执行NonQuery命令
/// </summary>
/// <param name="cmdTxt"></param>
/// <param name="parames"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string cmdTxt, params SqlParameter[] parames)
{
return ExecuteNonQuery(cmdTxt, CommandType.Text, parames);
}
//可以使用存储过程的ExecuteNonquery
public static int ExecuteNonQuery(string cmdTxt, CommandType cmdtype, params SqlParameter[] parames)
{
//判断脚本是否为空 ,直接返回0
if (string.IsNullOrEmpty(cmdTxt))
{
return 0;
}
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
if (parames != null)
{
cmd.CommandType = cmdtype;
cmd.Parameters.AddRange(parames);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
public static SqlDataReader ExecuteDataReader(string cmdTxt, params SqlParameter[] parames)
{
return ExecuteDataReader(cmdTxt, CommandType.Text, parames);
}
//SQLDataReader存储过程方法
public static SqlDataReader ExecuteDataReader(string cmdTxt, CommandType cmdtype, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return null;
}
SqlConnection con = new SqlConnection(str);
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
cmd.CommandType = cmdtype;
if (parames != null)
{
cmd.Parameters.AddRange(parames);
}
con.Open();
//把reader的行为加进来。当reader释放资源的时候,con也被一块关闭
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parames)
{
return ExecuteDataTable(sql, CommandType.Text, parames);
}
//调用存储过程的类,关于(ExecuteDataTable)
public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(sql))
{
return null;
}
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(sql, str))
{
da.SelectCommand.CommandType = cmdType;
if (parames != null)
{
da.SelectCommand.Parameters.AddRange(parames);
}
da.Fill(dt);
return dt;
}
}
/// <summary>
/// ExecuteScalar
/// </summary>
/// <param name="cmdTxt">第一个参数,SQLServer语句</param>
/// <param name="parames">第二个参数,传递0个或者多个参数</param>
/// <returns></returns>
public static object ExecuteScalar(string cmdTxt, params SqlParameter[] parames)
{
return ExecuteScalar(cmdTxt, CommandType.Text, parames);
}
//可使用存储过程的ExecuteScalar
public static object ExecuteScalar(string cmdTxt, CommandType cmdtype, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return null;
}
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
cmd.CommandType = cmdtype;
if (parames != null)
{
cmd.Parameters.AddRange(parames);
}
con.Open();
return cmd.ExecuteScalar();
}
}
}
//调用存储过程的DBHelper类(关于ExeceutScalar,包含事务,只能处理Int类型,返回错误号)
public static object ExecuteScalar(string cmdTxt, CommandType cmdtype,SqlTransaction sqltran, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return 0;
}
using (SqlConnection con = new SqlConnection(str))
{
int sum = 0;
using (SqlCommand cmd = new SqlCommand(cmdTxt, con))
{
cmd.CommandType=cmdtype;
if (parames != null)
{
cmd.Parameters.AddRange(parames);
}
con.Open();
sqltran = con.BeginTransaction();
try
{
cmd.Transaction = sqltran;
sum=Convert.ToInt32( cmd.ExecuteScalar());
sqltran.Commit();
}
catch (SqlException ex)
{
sqltran.Rollback();
}
return sum;
}
}
}
}
}
//业务逻辑层
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySchool.DAL;
using MySchool.Models;
namespace MySchool.BLL
{
public class StudentBLL
{
StudentDAL DAL=new StudentDAL();
public bool insert(string studentname, string pwd)
{
return DAL.insert(studentname, pwd);
}
public bool insertStudent(string LoginPwd, string StudentName, string Gender, int GradeId, string Phone,
string Address, DateTime Birthday, string Email)
{
return DAL.insertStudent(LoginPwd, StudentName, Gender, GradeId, Phone,
Address, Birthday, Email);
}
public DataTable Selectname(string name)
{
return DAL.Selectname(name);
}
public DataTable SelectStudentName(string name)
{
return DAL.SelectStudentName(name);
}
public DataTable SelectStudentGradeid(string Gradename)
{
return DAL.SelectStudentGradeid(Gradename);
}
public DataTable allStudent()
{
return DAL.allStudent();
}
public StudentModels getSelectStudentInfo(string StuNo)
{
return DAL.getSelectStudentInfo(StuNo);
}
public List<Student> get()
{
return DAL.get();
}
public DataTable selectduo()
{
return DAL.selectduo();
}
//public int Getidbyname(string gradename)
//{
//
//}
public DataTable selectduo1(int gradeid)
{
return DAL.selectduo1(gradeid);
}
public int Getidbyname(string gradename)
{
return DAL.Getidbyname(gradename);
}
public bool xiugaimima(StudentModels stu)
{
return DAL.xiugaimima(stu);
}
public int shanchu(string shu)
{
return DAL.shanchu(shu);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySchool.DAL;
namespace MySchool.BLL
{
public class GradeBLL
{
GradeidDal dal=new GradeidDal();
public DataTable allGradeID()
{
return dal.allGradeID();
}
public int Gradeidall(string Name)
{
return dal.Gradeidall(Name);
}
}
}
//表示层
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 MySchool.BLL;
namespace MySchool.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StudentBLL sd = new StudentBLL();
string username = txtStudentName.Text;
string Pwd = txtPwd.Text;
bool flag = sd.insert(username,Pwd);
if (flag)
{
FrmMain frm=new FrmMain();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("登录失败");
}
}
private void Form1_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 MySchool.BLL;
using MySchool.Models;
namespace MySchool.UI
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
StudentBLL stu = new StudentBLL();
public string StuNo;
private void Form2_Load(object sender, EventArgs e)
{
StudentModels stu1= stu.getSelectStudentInfo(StuNo);
txtStudentNo.Text=stu1.StudentNo;
txtName.Text = stu1.StudentName;
txtPwd.Text = stu1.LoginPwd;
txtEmail.Text = stu1.Email;
txtPhone.Text = stu1.Phone;
txtAddress.Text = stu1.Address;
txtEmail.Text = stu1.Email;
txtRePwd.Text = stu1.LoginPwd;
// cboGrade.Text = gd.Gradeidall(stu1.GradeId);
dpBirthday.Text = stu1.Birthday.ToString();
DataTable dt= gd.allGradeID();
cboGrade.DataSource = dt;
cboGrade.ValueMember = "Gradeid";
cboGrade.DisplayMember = "GradeName";
}
GradeBLL gd = new GradeBLL();
StudentBLL sd = new StudentBLL();
private void btnEdit_Click(object sender, EventArgs e)
{
//Student stu = sd.GetStudentInfo();
// sd.GetStudentInfo();
string pwd = txtPwd.Text;
string uname = txtName.Text;
string Gender = rbtnFemale.Checked ? "1" : "0";
string gradename = cboGrade.Text;
int gradeid = gd.Gradeidall(gradename);
string phone = txtPhone.Text;
string address = txtAddress.Text;
DateTime birthday = dpBirthday.Value;
string email = txtEmail.Text;
bool flag = sd.insertStudent(pwd, uname, Gender, gradeid, phone, address, birthday, email);
if (flag)
{
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
}
}
private void button1_Click(object sender, EventArgs e)
{
StudentModels sm=new StudentModels();
StuNo=txtStudentNo.Text;
sm.StudentNo=txtStudentNo.Text;
sm.LoginPwd = txtPwd.Text;
sm.StudentName = txtName.Text;
sm.Gender = rbtnFemale.Checked ? "1" : "0";
sm.gradename = cboGrade.Text;
sm.GradeId = gd.Gradeidall(sm.gradename);
sm.Phone = txtPhone.Text;
sm.Address = txtAddress.Text;
sm.Birthday = dpBirthday.Value;
sm.Email = txtEmail.Text;
bool flag = sd.xiugaimima(sm);
if (flag)
{
MessageBox.Show("修改成功");
}
else
{
MessageBox.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 MySchool.BLL;
namespace MySchool.UI
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StudentBLL ds = new StudentBLL();
string name = textBox1.Text;
if (textBox1.Text!="")
{
DataTable da= ds.SelectStudentName(name);
dataGridView1.DataSource = da;
}
else
{
MessageBox.Show("请输入正确的姓名");
}
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void 修改ToolStripMenuItem_Click(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 MySchool.BLL;
namespace MySchool.UI
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
GradeBLL gd = new GradeBLL();
private void Form4_Load(object sender, EventArgs e)
{
DataTable dt = gd.allGradeID();
comboBox1.DataSource = dt;
comboBox1.ValueMember = "Gradeid";
comboBox1.DisplayMember = "GradeName";
}
private void button1_Click(object sender, EventArgs e)
{
StudentBLL ds = new StudentBLL();
string name = comboBox1.Text;
if (comboBox1.Text != "")
{
DataTable da = ds.SelectStudentGradeid(name);
dataGridView1.DataSource = da;
}
else
{
MessageBox.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 MySchool.BLL;
using MySchool.Models;
namespace MySchool.UI
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
StudentBLL st = new StudentBLL();
DataTable ds = st.allStudent();
dataGridView1.DataSource = ds;
}
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2=new Form2();
f2.StuNo= dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
f2.Show();
this.Hide();
}
private void Form5_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = dataGridView1.SelectedCells[0].Value.ToString();
StudentBLL da = new StudentBLL();
int i = da.shanchu(str);
if (i > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.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 MySchool.BLL;
namespace MySchool.UI
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
StudentBLL st = new StudentBLL();
private void Form6_Load(object sender, EventArgs e)
{
dataGridView1.DataSource=st.get();
comboBox1.DataSource= st.selectduo();
comboBox1.ValueMember = "gradeid";
comboBox1.DisplayMember = "gradename";
}
private void button1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string gradename = comboBox1.Text;
int gradeid = st.Getidbyname(gradename);
comboBox2.DataSource = st.selectduo1(gradeid);
comboBox2.ValueMember = "subjectid";
comboBox2.DisplayMember = "subjectname";
}
}
}
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 MySchool.UI
{
public partial class FrmMain : Form
{
public string userName;
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void FrmMain_Load(object sender, EventArgs e)
{
STA1.Text = "工作日期:" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongDateString();
STA2.Text = "welcome" + userName;
}
private void tsbtnNewStudent_Click(object sender, EventArgs e)
{
Form2 f2=new Form2();
f2.Show();
this.Hide();
}
private void tsbtnStudentByGrade_Click(object sender, EventArgs e)
{
Form4 f4=new Form4();
f4.Show();
}
private void tsbtnStudentByName_Click(object sender, EventArgs e)
{
Form3 f3=new Form3();
f3.Show();
}
private void tsbtnStudentList_Click(object sender, EventArgs e)
{
Form5 F5 = new Form5();
F5.Show();
}
private void 多表查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form6 F5 = new Form6();
F5.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySchool.Models;
namespace MySchool.DAL
{
public class StudentDAL
{
public bool insert(string studentname,string pwd)
{
bool flag = false;
string sql = "select count(*) from student where studentname='" + studentname + "'and LoginPwd='" + pwd + "'";
int count = Convert.ToInt32(sqlHelper.ExecuteScalar(sql));
if (count>0)
{
flag = true;
}
return flag;
}
//LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email
public bool insertStudent(string LoginPwd,string StudentName,string Gender, int GradeId,string Phone,string Address,DateTime Birthday,string Email)
{
bool flag = false;
string sql = @"insert into Student
(LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email) values('" + LoginPwd + "','" + StudentName + "','" + Gender + "','" + GradeId + "','" + Phone + "','" + Address + "','" + Birthday + "','" + Email + "')";
int count= sqlHelper.ExecuteNonQuery(sql);
if (count>0)
{
flag = true;
}
return flag;
}
public DataTable Selectname(string name)
{
string str = sqlHelper.str;
SqlConnection con=new SqlConnection(str);
string sql = "select * from Student where Studentname=@name";
SqlParameter para=new SqlParameter("@name",name);
SqlCommand cmd=new SqlCommand(sql,con);
cmd.Parameters.Add(para);
SqlDataAdapter sqa=new SqlDataAdapter();
sqa.SelectCommand = cmd;
DataSet ds=new DataSet();
sqa.Fill(ds, "info");
return ds.Tables["info"];
}
public DataTable SelectStudentName(string name)
{
string sql = "select * from Student where StudentName='"+name+"'";
return sqlHelper.ExecuteDataTable(sql);
}
public DataTable SelectStudentGradeid(string Gradename)
{
string sql = "select * from Student where Gradeid in (select Gradeid from Grade where GradeName='"+Gradename+"')";
return sqlHelper.ExecuteDataTable(sql);
}
public DataTable allStudent()
{
string sql = "select * from Student";
return sqlHelper.ExecuteDataTable(sql);
}
public StudentModels getSelectStudentInfo(string StuNo)
{
string sql = "select * from student where StudentNo='" + StuNo + "'";
SqlDataReader dr = sqlHelper.ExecuteDataReader(sql);
StudentModels stu=new StudentModels();
while (dr.Read())
{
stu.StudentName = dr["StudentName"].ToString();
stu.LoginPwd = dr["LoginPwd"].ToString();
stu.StudentName = dr["StudentName"].ToString();
stu.GradeId = Convert.ToInt32(dr["GradeId"]);
stu.Gender = dr["Gender"].ToString();
stu.Phone = dr["Phone"].ToString();
stu.Birthday = (DateTime) dr["Birthday"];
stu.Address = dr["Address"].ToString();
stu.Email = dr["Email"].ToString();
}
return stu;
}
public bool xiugaimima(StudentModels stu)
{
//string str = SqlHelper.Constr;
//SqlConnection con=new SqlConnection(str);
string sql = "update student set loginpwd='" + stu.LoginPwd + "'where studentname='" + stu.StudentName + "'";
//SqlCommand cmd=new SqlCommand(sql,con);
//con.Open();
int i = sqlHelper.ExecuteNonQuery(sql);
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public List<Student> get()
{
List<Student> list = new List<Student>();
string str = sqlHelper.str;
string sql =
"select studentname,subjectname,studentresult from student,subject,result where student.studentno=result.studentno and subject.subjectid=result.subjectid";
DataTable dt = sqlHelper.ExecuteDataTable(sql);
foreach (DataRow row in dt.Rows)
{
//一个StudentEx对象
Student stu = new Student();
stu.StudentName = row["studentname"].ToString();
stu.subjectname = row["subjectname"].ToString();
stu.studentresult = Convert.ToInt32(row["studentresult"]);
list.Add(stu);
}
return list;
}
public DataTable selectduo()
{
string str = sqlHelper.str;
string sql = "select * from Grade";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter sqa = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sqa.Fill(ds, "info");
return ds.Tables["info"];
}
public DataTable selectduo1(int gradeid)
{
string str = sqlHelper.str;
string sql = "select * from subject where gradeid=@gradeid";
SqlConnection con = new SqlConnection(str);
SqlCommand com = new SqlCommand(sql, con);
SqlParameter sp = new SqlParameter("gradeid", gradeid);
com.Parameters.Add(sp);
SqlDataAdapter sqa = new SqlDataAdapter();
sqa.SelectCommand = com;
DataSet ds = new DataSet();
sqa.Fill(ds, "info");
if (ds.Tables["info"].Rows.Count == 0)
{
ds.Tables["info"].Clear();
}
return ds.Tables["info"];
}
public int Getidbyname(string gradename)
{
string str = sqlHelper.str;
string sql = "select gradeid from grade where gradename='" + gradename + "'";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand com = new SqlCommand(sql, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
return count;
}
public int shanchu(string shu)
{
//bool flag = false;
//string str = SqlHelper.Constr;
//SqlConnection con=new SqlConnection(str);
string sql = "delete from student where studentno='" + shu + "'";
//con.Open();
//SqlCommand com=new SqlCommand(sql,con);
//con.Close();
int i = sqlHelper.ExecuteNonQuery(sql);
return i;
}
}
}