编辑: huangshuowei01 | 2018-01-18 |
业务逻辑层是对系统业务实体的封装,完成系统业务功能;
数据访问层直接与数据库打交道,为业务逻辑层提供底层的数据库操作. 5.1 Database类主要是与数据库连接,提供数据库操作功能,代码如下: namespace MyElectCourse.DAL { public class Database { protected string connectionString;
protected SqlConnection connection = null;
public Database() { connectionString = ConfigurationManager.ConnectionStrings[ ConnectionString ].ConnectionString.ToString();
} ~Database() { if (connection != null) { connection = null;
} } protected void Open() { if (connection == null) { connection = new SqlConnection(connectionString);
} if (connection.State.Equals(ConnectionState.Closed)) { connection.Open();
} } protected void Close() { if (connection != null) { connection.Close();
} } public int ExecuteSQL(string sqlstr) { int count = -1;
this.Open();
SqlCommand cmd = new SqlCommand(sqlstr, connection);
count = cmd.ExecuteNonQuery();
this.Close();
return count;
} public DataSet GetDataSet(string sqlstr) { this.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sqlstr,connection);
adapter.Fill(ds);
this.Close();
return ds;
} public DataTable GetDataTable(string sqlstr) { DataSet ds = this.GetDataSet(sqlstr);
DataTable dt = new DataTable();
if (ds.Tables.Count >
0) { dt = ds.Tables[0];
} return dt;
} public SqlDataReader GetDataReader(string sqlstr) { this.Open();
SqlCommand cmd = new SqlCommand(sqlstr, connection);
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//this.Close();
return sdr;
} } } 5.2 UserBase类是所有系统角色用户的基类,完成用户登录验证与修改密码的功能,代码如下: namespace MyElectCourse.BLL { public class UserBase { private string userID;
public string UserID { get { return userID;
} set { userID = value;
} } private string userPSW;
public string UserPSW { get { return userPSW;
} set { userPSW = value;
} } public string loginCheck(string uid, string upwd, string urole) { String selectStr = String.Empty;
switch (urole) { case
0 身份为教师时 selectStr = Select * from Teacher where teaID = '
+ uid + '
;
break;
case
1 身份为学生时 selectStr = Select * from Student where stuID = '
+ uid + '
;
break;
case
2 身份为管理员时 selectStr = Select * from Users where adminName = '
+ uid + '
;
break;
default: return null;
} Database db = new Database();
DataTable dt = db.GetDataTable(selectStr);
if (dt.Rows.Count >
0)如果该用户存在 { if (dt.Rows[0][1].ToString().Equals(upwd)) //密码正确 { switch (urole) { case
0 身份为教师时 return
0 ;
case
1 身份为学生时 return
1 ;
case
2 身份为管理员时 return
2 ;
default: return null;
} } else //密码错误,给出提示信息! { return -1 ;
} } else //用户不存在或用户名输入错误 { return -2 ;
} } public string modifyPWD(String urole, String uid, String oldPwd, String newPwd) { String updateStr = String.Empty;
switch (urole) { case
0 身份为教师时 updateStr = update Teacher set teaPwd='
+ newPwd + '
where teaID='
+ uid + '
;
break;
case
1 身份为学生时 updateStr = update Student set stuPwd='
+ newPwd + '
where stuID='
+ uid + '
;
break;
case
2 身份为管理员时 updateStr = update Users set adminPwd='