首先 , 现说一下 一共有4个项目 分别是
Myschool.DAL MySchool.Models Myschool.BLL MySchool.UI
关系: MySchool.UI---->Myschool.BLL---->Myschool.DAL
Myschool.DAL 中我写了4个类 包括 StudentDAL GradeDAL SQLHelper SubjectDAL
GradeDAL 内容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace MySchool.DAL
{
public class GradeDAL
{
//获取所有年级信息
public DataTable GetFullDate()
{
string str = SqlHelper.constr;
SqlConnection con = new SqlConnection(str);
string sql = "select gradeid,gradename from grade";
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sql,con);
adapter.Fill(ds,"GradeInfo");
return ds.Tables["GradeInfo"];
}
//根据id获取年级名称
public int GetidByName(string gradename)
{
string sql = "select gradeid from grade where gradename='" + gradename + "'";
int gradeid = Convert.ToInt32(SqlHelper.ExecuteScalar(sql));
return gradeid;
}
}
}
StudentDAL 内容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using MySchool.Mode;
namespace MySchool.DAL
{
public class StudentDAL
{
//添加学生信息
public bool AddStudent(string pwd,string uname,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 ( '"+pwd+"','"+uname+"','"+gender+"','"+gradeid+"','"+phone+"','"+address+"','"+DateTime.Now.ToShortDateString()+"','"+email+"')";
int count = Convert.ToInt32(SqlHelper.ExecuteNonQuery(sql));
if (count > 0)
{
flag = true;
}
return flag;
}
//登录
public bool isLogin(string username,string pwd)
{
bool flag = false;
string sql = "select count(1) from student where studentname='" + username + "'and LoginPwd='" + pwd + "'";
int count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql));
if(count>0)
{
flag = true;
}
return flag;
}
//显示信息
public DataTable Show()
{
string sql = SqlHelper.constr;
SqlConnection con = new SqlConnection(sql);
string str = "select StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email from student";
SqlDataAdapter Adapter = new SqlDataAdapter(str, con);
DataSet da = new DataSet();
Adapter.Fill(da, "pp");
return da.Tables["pp"];
}
//年级查询
public DataTable SelectStudentGradeid(string Gradename)
{
string con = SqlHelper.constr;
string sql = "select * from Student where Gradeid in (select Gradeid from Grade where GradeName='" + Gradename + "')";
SqlConnection conn = new SqlConnection(con);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Gradeinfo");
return ds.Tables["Gradeinfo"];
}
//根据姓名查询
public DataTable SelectStudentNameAgoo(string name)
{
string con = SqlHelper.constr;
string sql = "select * from Student where StudentName=@name";
SqlConnection conn = new SqlConnection(con);
SqlParameter para = new SqlParameter("@name", name);
SqlCommand com = new SqlCommand(sql,conn);
com.Parameters.Add(para);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds, "Gradeinfo");
return ds.Tables["Gradeinfo"];
}
public DataTable SelectStudentName(string name)
{
string con = SqlHelper.constr;
string sql = "select * from Student where StudentName like '%'+@name+'%'";
SqlConnection conn = new SqlConnection(con);
SqlParameter para = new SqlParameter("@name", name);
SqlCommand com = new SqlCommand(sql, conn);
com.Parameters.Add(para);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds, "Gradeinfo");
return ds.Tables["Gradeinfo"];
}
//修改
public bool UpdataStudent(string pwd, string gender, int gradeid, string phone, string address, DateTime birthday, string email, string x)
{
bool blag = false;
string sql = SqlHelper.constr;
SqlConnection con = new SqlConnection(sql);
string str = "update student set LoginPwd='" + pwd + "',Gender='" + gender + "',phone='" + phone + "',address='" + address + "',birthday='" + birthday + "',email='" + email + "'where studentno='" + x + "'";
con.Open();
SqlCommand com = new SqlCommand(str, con);
int count = com.ExecuteNonQuery();
if (count > 0)
{
blag = true;
}
return blag;
}
//删除学生信息
public bool Delect(string studentNo)
{
bool f = false;
string con = SqlHelper.constr;
string sql = "DELETE FROM Student WHERE [StudentNo]='" + studentNo + "'";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int count = cmd.ExecuteNonQuery();
conn.Close();
if (count > 0)
{
f = true;
}
return f;
}
//传值修改信息
public List<Student> GetStudentByStudentno(string studentno)
{
string constr = SqlHelper.constr;
string sql = "select * from student where studentno='" + studentno + "'";
SqlConnection con = new SqlConnection(constr);
List<Student> studentList = new List<Student>();
try
{
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Student student = new Student();
student.LoginPwd = dr["LoginPwd"].ToString();
student.StudentName = dr["StudentName"].ToString();
student.Gender = dr["Gender"].ToString();
student.GradeId = dr["GradeId"].ToString();
student.Phone = dr["Phone"].ToString();
student.Address = dr["Address"].ToString();
student.Email = dr["email"].ToString();
student.Birthday = Convert.ToDateTime(dr["Birthday"]);
studentList.Add(student);
}
dr.Close();
return studentList;
}
catch (Exception)
{
throw;
}
}
//带参
public DataTable get(string name , int gid,out int i,out int j)
{
string strcon = SqlHelper.constr;
SqlConnection con = new SqlConnection(strcon);
string sql = "usp_daican";
SqlCommand com = new SqlCommand(sql,con);
SqlParameter[] paras =
{
new SqlParameter("@gender",name),
new SqlParameter("@gradeid",gid),
new SqlParameter("@countnum",SqlDbType.Int),
new SqlParameter("@return",SqlDbType.Int)
};
paras[2].Direction=ParameterDirection.Output;
paras[3].Direction = ParameterDirection.ReturnValue;
com.Parameters.AddRange(paras);
com.CommandType=CommandType.StoredProcedure;
SqlDataAdapter ad=new SqlDataAdapter ();
ad.SelectCommand=com;
DataSet ds=new DataSet ();
ad.Fill(ds,"studentinfo");
i=Convert.ToInt32(paras[2].Value);
j = Convert.ToInt32(paras[3].Value);
Console.WriteLine(i);
Console.WriteLine(j);
return ds.Tables["studentinfo"];
}
}}
SQLHelper 内容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace MySchool.DAL
{
public class SqlHelper
{
// public static string constr = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
public static string constr = ConfigurationManager.ConnectionStrings["constr"].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(constr))
{
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(constr);
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, constr))
{
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(constr))
{
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(constr))
{
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;
}
}
}
}
}
SubjectDAL 内容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace MySchool.DAL
{
public class SubjectDAL
{
public DataTable Show()
{
string sql = SqlHelper.constr; ;
SqlConnection con = new SqlConnection(sql);
string str = "select SubjectId, SubjectName, ClassHour, GradeId from subject";
SqlDataAdapter Adapter = new SqlDataAdapter(str, con);
DataSet da = new DataSet();
Adapter.Fill(da, "subject");
return da.Tables["subject"];
}
}
}
MySchool.BLL 项目中包括 GradeBLL StudentBLL SubjectBLL
GradeBLL 代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySchool.DAL;
using System.Data;
namespace MySschool.BLL
{
public class GradeBLL
{
GradeDAL grade = new GradeDAL();
//获取所有年级信息
public DataTable GetFullDate()
{
return grade.GetFullDate();
}
//根据id获取年级名称
public int GetidByName(string gradename)
{
return grade.GetidByName(gradename);
}
}
}
StudentBLL 代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySchool.DAL;
using System.Data;
using MySchool.Mode;
namespace MySschool.BLL
{
public class StudentBLL
{
//登陆
StudentDAL stu = new StudentDAL();
public bool isLogin(string username, string pwd)
{
return stu.isLogin(username, pwd);
}
//添加学生信息
public bool AddStudent(string pwd, string uname, string gender, int gradeid, string phone, string address, DateTime birthday, string email)
{
return stu.AddStudent(pwd, uname, gender, gradeid, phone, address, birthday, email);
}
//显示信息
public DataTable Show()
{
return stu.Show();
}
//年级查询
public DataTable SelectStudentGradeid(string Gradename)
{
return stu.SelectStudentGradeid(Gradename);
}
//根据姓名查询
public DataTable SelectStudentNameAgoo(string name)
{
return stu.SelectStudentNameAgoo(name);
}
public DataTable SelectStudentName(string name)
{
return stu.SelectStudentName(name);
}
//修改
public bool UpdataStudent(string pwd, string gender, int gradeid, string phone, string address, DateTime birthday, string email, string x)
{
return stu.UpdataStudent(pwd, gender, gradeid, phone, address, birthday, email, x);
}
//删除学生信息
public bool Delect(string studentNo)
{
return stu.Delect(studentNo);
}
//传值修改信息
public List<Student> GetStudentByStudentno(string studentno)
{
return stu.GetStudentByStudentno(studentno);
}
//带参
public DataTable get(string name, int gid, out int i, out int j)
{
return stu.get(name,gid,out i,out j);
}
}
}
SubjectBLL 代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySchool.DAL;
using System.Data;
namespace MySschool.BLL
{
public class SubjectBLL
{
SubjectBLL sub = new SubjectBLL();
public DataTable Show()
{
return sub.Show();
}
}
}
MySchool.Models 项目中有Student Grade Subject Result
Student类的代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySchool.Mode
{
public class Student
{
public string StudentNo { get; set; }
public string StudentName { get; set; }
public string LoginPwd { get; set; }
public string GradeId { get; set; }
public string Gender { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public DateTime Birthday { get; set; }
//public Student() { }
//public Student(string studentno,string studentname,string pwd,string gradeid,string gender,string phone,string address,string email,DateTime birthday)
//{
//}
}
}
Grade类中的代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySchool.Mode
{
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
}
}
Subject 类代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySchool.Mode
{
public class Subject
{
public int SubjectId { get; set; }
public string SubjectName { get; set; }
public int ClassHour { get; set; }
public int GradeId { get; set; }
}
}
Result 类中的代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySchool.Mode
{
public class Result
{
public int StudentNo { get; set; }
public int SubjectId { get; set; }
public int StudentResult { get; set; }
public DateTime ExamDate { get; set; }
public int Id { get; set; }
}
}
最后是MySchool.UI 项目 这是窗体项目
这是登陆窗体 点击登陆的代码如下
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.UI;
using MySschool.BLL;
namespace MySchool.UI
{
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
StudentBLL dal = new StudentBLL();
string userName = txtusername.Text;
string pwd = txtpwd.Text;
bool flag=dal.isLogin(userName,pwd);
if (flag)
{
FrmMain frm = new FrmMain();
frm.userName = txtusername.Text;
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("登录失败");
}
}
}
}
这是登陆成功后的主窗体
注意的是 这里有个contexttrip控件
点击新增学生 后跳转到新增窗体
这个窗体的代码
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;
using MySchool.Mode;
using MySschool.BLL;
namespace MySchool.UI
{
public partial class FrmAddStudent : Form
{
public FrmAddStudent()
{
InitializeComponent();
}
public string studentNo=string.Empty;
StudentBLL sdal = new StudentBLL();
GradeBLL gdal = new GradeBLL();
private void btnEdit_Click(object sender, EventArgs e)
{
//密码
string pwd = txtPwd.Text;
//姓名
string uname = txtName.Text;
//性别
string gender = rbtnMale.Checked ? "1" : "0";
//年级
string gradename = cboGrade.Text;
int gradeid = Convert.ToInt32(gdal.GetidByName(gradename));
//电话
string phone = txtPhone.Text;
//地址
string address = txtAddress.Text;
//出生日期
DateTime birthday = dpBirthday.Value;
//邮箱
string email = txtEmail.Text;
bool flag = sdal.AddStudent(pwd,uname,gender,gradeid,phone,address,birthday,email);
if (flag)
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("失败");
}
}
private void FrmAddStudent_Load(object sender, EventArgs e)
{
DataTable dt = gdal.GetFullDate();
cboGrade.DataSource = dt;
cboGrade.ValueMember = "gradeid";
cboGrade.DisplayMember = "gradename";
txtStudentNo.Text = studentNo;
}
//清空
private void btnClear_Click(object sender, EventArgs e)
{
foreach (Control item in gbUserLoginInfo.Controls)
{
if (item is TextBox)
{
item.Text = "";
}
}
foreach (Control item in gbUserInfo.Controls)
{
if (item is TextBox || item is ComboBox)
{
item.Text = "";
}
}
}
//修改
private void btnxiugai_Click(object sender, EventArgs e)
{
string pwd = this.txtPwd.Text;
string greade = string.Empty;
if (rbtnFemale.Checked)
{
greade = "0";
}
else
{
greade = "1";
}
string phone = this.txtPhone.Text;
string address = this.txtAddress.Text;
string email = this.txtEmail.Text;
DateTime dateTime = this.dpBirthday.Value;
string gradename = this.cboGrade.Text;
int gradeid = gdal.GetidByName(gradename);
bool s = sdal.UpdataStudent(pwd, greade, gradeid, phone, address, dateTime, email, studentNo);
if (s)
{
MessageBox.Show("修改成功!");
this.Hide();
}
else
{
MessageBox.Show("失败");
}
}
private void txtPwd_TextChanged(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 MySschool.BLL;
namespace MySchool.UI
{
public partial class FrmSearchStudentByName : Form
{
public FrmSearchStudentByName()
{
InitializeComponent();
}
StudentBLL dal = new StudentBLL();
private void btnSearch_Click(object sender, EventArgs e)
{
string name = this.txtStudentName.Text;
DataTable dataTable = dal.SelectStudentName(name);
dataGridView1.DataSource = dataTable;
}
private void tsmiEdit_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 MySschool.BLL;
namespace MySchool.UI
{
public partial class FrmSearchStudentByGrade : Form
{
public FrmSearchStudentByGrade()
{
InitializeComponent();
}
private void btnSearch_Click(object sender, EventArgs e)
{
StudentBLL ds = new StudentBLL();
string name = cboGrade.Text;
if (cboGrade.Text != "")
{
DataTable da = ds.SelectStudentGradeid(name);
dgvStuName.DataSource = da;
}
else
{
MessageBox.Show("请输入正确的年级");
}
}
GradeBLL gd = new GradeBLL();
private void FrmSearchStudentByGrade_Load(object sender, EventArgs e)
{
string grandename = cboGrade.Text;
DataTable dt = gd.GetFullDate();
cboGrade.DataSource = dt;
cboGrade.ValueMember = "Gradeid";
cboGrade.DisplayMember = "GradeName";
}
private void tsmiAddResult_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 MySschool.BLL;
namespace MySchool.UI
{
public partial class FrmStudentInfoList : Form
{
public FrmStudentInfoList()
{
InitializeComponent();
}
FrmAddStudent frmadd = new FrmAddStudent();
StudentBLL dal = new StudentBLL();
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string x = dgvlist.SelectedRows[0].Cells[0].Value.ToString();
bool delete = dal.Delect(x);
if (delete)
{
DataTable dataTable = dal.Show();
dgvlist.DataSource = dataTable;
MessageBox.Show("删除成功");
}
}
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmadd.studentNo = dgvlist.SelectedRows[0].Cells[0].Value.ToString();
frmadd.Show();
}
private void InfoList_Load(object sender, EventArgs e)
{
DataTable dataTable = dal.Show();
dgvlist.DataSource = dataTable;
}
}
}
这是几个主要窗体和代码 剩下的就不写了