根据角色权限确定
private void Main_Load(object sender, EventArgs e)
{
//权限控制
try
{
string strConn1 = "数据库连接字符串";
SqlConnection myConn1 = new SqlConnection(strConn1);
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = myConn1;
cmd1.CommandText = "数据库查询命令语句,查询权限表角色权限";
SqlDataAdapter adapter1 = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
if (adapter1.Fill(ds) == 0)
{
MessageBox.Show("请重新登录!");
}
else//通过上面查询的数据鉴别所有下拉按键的显示与否,实现不同角色只能显示该角色能获取的功能按钮
{
if (ds.Tables[0].Rows[0]["属性名,例如addtea"].ToString() == "0")
t1addtea.Available = false;//如果所查属性值为0,则不显示下拉按钮t1addtea
if (ds.Tables[0].Rows[0]["deltea"].ToString() == "0")
t2deltea.Available = false;
if (ds.Tables[0].Rows[0]["addcou"].ToString() == "0")
t3addcou.Available = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
根据数据库表内容确定是否勾选checkbox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string strConn1 = "数据库连接字符串";
SqlConnection myConn1 = new SqlConnection(strConn1);
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = myConn1;
cmd1.CommandText = "select * from pertable where roleid='" + comboBox2.Text + "'";
SqlDataAdapter adapter1 = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
if (adapter1.Fill(ds) == 0)
{
}
else
{
if (ds.Tables[0].Rows[0]["addtea"].ToString() == "0")
addtea1.Checked = false;
else
addtea1.Checked = true;
if (ds.Tables[0].Rows[0]["deltea"].ToString() == "0")
deltea2.Checked = false;
else
deltea2.Checked = true;
if (ds.Tables[0].Rows[0]["addcou"].ToString() == "0")
addcou3.Checked = false;
else
addcou3.Checked = true;
if (ds.Tables[0].Rows[0]["delcou"].ToString() == "0")
delcou4.Checked = false;
else
delcou4.Checked = true;
if (ds.Tables[0].Rows[0]["addstu"].ToString() == "0")
addstu5.Checked = false;
else
addstu5.Checked = true;
if (ds.Tables[0].Rows[0]["delstu"].ToString() == "0")
delstu6.Checked = false;
else
delstu6.Checked = true;
if (ds.Tables[0].Rows[0]["addgra"].ToString() == "0")
addgra7.Checked = false;
else
addgra7.Checked = true;
if (ds.Tables[0].Rows[0]["chegra"].ToString() == "0")
chegra8.Checked = false;
else
chegra8.Checked = true;
if (ds.Tables[0].Rows[0]["cheagra"].ToString() == "0")
cheagra9.Checked = false;
else
cheagra9.Checked = true;
if (ds.Tables[0].Rows[0]["chapwd"].ToString() == "0")
chapwd10.Checked = false;
else
chapwd10.Checked = true;
if (ds.Tables[0].Rows[0]["rigman"].ToString() == "0")
rigman11.Checked = false;
else
rigman11.Checked = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}