说明:由于时间仓促,代码比较粗糙,若哪里不好请您原谅,并且改正,希望下面的代码能帮到您.
思路:管理员一个表,学生一个表,这样才不会干扰,操作也方便
界面设计:两个文本框name属性值为 txtName ,txtPassWord
一个下拉框name属性值为cm,下拉框首先要编辑它的项,可以右击下拉框选中”编辑项”进行编辑 一个按钮
数据库
1.新建一个数据库,库名为 Student
2.新建一个管理员登录信息的表, 在表中填入列名一列为ManName,另一列为ManPwd,保存 表名为Manage
3.新建一个学生登陆信息的表,在表中填入列名一列为StuLogInName,另一列为StuLogInPws,保存 表名为StuLogIn
3.打开表 ,在表中填入一条或多条记录
private void btnOK_Click(object sender, EventArgs e)
{
if (txtName.Text.Length < 1 || txtPassWord.Text.Length < 1)
{
MessageBox.Show("请填写帐号或密码");
}
else
{
if (cm.SelectedIndex == 0) //选择管理员登录
{
Power = "manage";
Myconnection();
}
else if (cm.SelectedIndex == 1) //选择学生登录
{
Power = "student";
Myconnection();
}
else // 当没有选择权限时,系统会提示选择
{
MessageBox.Show("请选择权限!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
}
}
SqlCommand command; //全局变量
public void Myconnection()
{
try
{
string ConStr="initial catalog=Student;Data source=.;integrated security=sspi;"; //创建连接字符串
SqlConnection connection=new SqlConnection (ConStr);//创建连接对象
string sqlManage = String.Format("select count(*) from Manage where ManName='{0}' and ManPwd='{1} '", txtName.Text, txtPassWord.Text);
string sqlStudent = String.Format("select count(*) from StuLogIn where StuLogInName='{0}' and StuLogInPws='{1}'",txtName.Text,txtPassWord.Text);
if (Power == "manage")
{
command = new SqlCommand(sqlManage, connection); //当权限选择 管理员 时 会查找 Manage 表
}
else
{
command = new SqlCommand(sqlStudent, connection); //当权限选择 学生 时 会查找 StuLogIn 表
}
int count = (int)command.ExecuteScalar();
if (count== 1) //这里的if else是将结果反馈给用户
{
MessageBox.Show("登录成功!","温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
IsClose = true;
this.Close();
}
else
{
MessageBox.Show("登录失败!","温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtName.Focus();
}
connection.Close();
}
catch (Exception Err) //处理错误操作,防止出现错误
{
MessageBox.Show("操作失败!" + Err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
你可以这样,在数据库中再添加一个字段level,管理员的level为1,用户的level为0,当输入正确的帐号和密码时,先判断一下帐号的level值,然后进入相应的页面和执行相应的操作.
首相把登陆成功后的那个form2写一个构造函数form2(string name)
登陆成功后 通过得到的用户名(管理员或user)初始化form2,form2里写一个函数,通过判断传入的用户名来赋予不同的权限。具体权限你可以通过button的viseable或enable属性控制。
代码就不写了吧
可以用这两个名称空间的东西来做啊
using
System.Security.Permissions;
using
System.Security.Cryptography;
登陆成功后赋予权限
ClassLibrary.TestAppCredentials
taCS
=
new
ClassLibrary.TestAppCredentials("ss",
new
string[]
{
ClassLibrary.Roles.Admin.ToString()
});
Thread.CurrentPrincipal
=
taCS;
PrincipalPermission
pPerm
=
new
PrincipalPermission(null,
ClassLibrary.Roles.Admin.ToString(),
true);
pPerm.Demand();
据个简单的例子,检查当前用户是否具有Admin权限来决定显示一个按钮:
this.btnShowAll.Visible
=
Thread.CurrentPrincipal.IsInRole(ClassLibrary.Roles.Admin.ToString());
当然了,这个Roles是你自己定义的一个枚举类型而已。。。
using
System;
using
System.Collections.Generic;
using
System.Collections.Specialized;
using
System.Linq;
using
System.Text;
using
System.Security;
using
System.Security.Principal;
using
System.Runtime.Serialization.Formatters.Binary;
using
System.Xml.Serialization;
using
System.IO;
namespace
ClassLibrary
{
public
enum
Roles
{
Admin,
Manager,
Soles,
None
}
public
class
TestAppCredentials:IPrincipal
{
private
GenericIdentity
_Identity;
private
StringCollection
_roles
=
new
StringCollection();
private
DateTime
_Created;
private
bool
_properlyInitted
=
false
public
TestAppCredentials(string
userName,
string[]
roles)
{
_Identity
=
new
GenericIdentity(userName,
"TestAppCredentials");
if
(null
!=
roles)
{
_roles.AddRange(roles);
}
_Created
=
DateTime.Now;
_properlyInitted
=
true;
}
#region
IPrincipal
成员
[XmlIgnoreAttribute]
public
IIdentity
Identity
{
get
{
CheckProperlyInitted();
CheckExpired();
return
_Identity;
}
}
首先要定义个权限类~!把它封装成dll然后调用,权限可以写在xml里面,程序启动,调用dll,去读取xml,这样就可以了!