设置用户角色,根据角色是、获取角色获取用户类型
登录可直接使用SqlDataReader取值

解决方案 »

  1.   

    设置权限
     //---------窗体创建时,将用户的权限数据读入并显示-----------
    private void PopedomManage_Load(object sender, System.EventArgs e)
    {
    this.ds = this.link.SelectDataBase(sendStrSQL,sendTableName);
    this.dgrd_Popedom.DataSource = ds.Tables[0];
    this.DataGridStateControl(); this.dgrd_Popedom.Select(0);//窗体生成后选中第一行
    string selectedUserID = this.ds.Tables[0].Rows[0][0].ToString();//默认选中用户清单中的第一个用户
    string tempStrSQL = "select 权限名称 from 权限清单 " + " where 用户编号 = '" + selectedUserID + "'";
    popedomDataTable = link.SelectDataBase(tempStrSQL);//读入该用户的权限
    for (int i=0;i<popedomDataTable.Rows.Count;i++)//根据权限设置ListBox
    {
    for (int j=0;j<this.chkLst_Priority.Items.Count;j++)
    {
    if (this.chkLst_Priority.Items[j].ToString().Trim() == popedomDataTable.Rows[i][0].ToString().Trim())
    {
    this.chkLst_Priority.SetItemChecked(j,true);
    }
    }
    }
    } //-------------设置显示用户信息的表---------------
    private void DataGridStateControl()
    {
    DataGridTableStyle ts = new DataGridTableStyle();
    DataGridNoActiveCellColumn aColumnTextColumn;//设置dataGrid格式
    ts.AlternatingBackColor = Color.LightGray;
    ts.MappingName = this.ds.Tables[0].TableName;
    ts.AllowSorting = false;
    int numCols = this.ds.Tables[0].Columns.Count;
    for (int i = 0;i< numCols;i++)
    {
    aColumnTextColumn = new DataGridNoActiveCellColumn();
    aColumnTextColumn.MappingName = this.ds.Tables[0].Columns[i].ColumnName;
    aColumnTextColumn.HeaderText = this.ds.Tables[0].Columns[i].ColumnName;
    aColumnTextColumn.NullText = "";
    aColumnTextColumn.Format = "F";
    ts.GridColumnStyles.Add(aColumnTextColumn);
    }
    this.dgrd_Popedom.TableStyles.Add(ts);
    }
    //-------------改变用户的权限,并向数据库中提交----------------
    private void chkLst_Priority_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
    {
    //CheckedListBox控件中的Item的CheckedState属性是由点击CheckedListBox控件而改变时才响应该事件
    if (blInitial == false)
    {
    if (e.CurrentValue.ToString() == "Unchecked")
    {
    try
    {
    string strUserName = this.dgrd_Popedom[this.dgrd_Popedom.CurrentCell.RowNumber,0].ToString().Trim();
    string strPopedom = this.chkLst_Priority.SelectedItem.ToString();
    string tempSendStrSQL = "insert 权限清单 (用户编号,权限名称) values ('" + strUserName +"','"+ strPopedom +"')";
    this.link.UpdateDataBase(tempSendStrSQL);
    }
    catch
    {
    MessageBox.Show("数据库中的权限修改出错,请重试!","信息");
    }
    }
    else if (e.CurrentValue.ToString() == "Checked")
    {
    try
    {
    string strUserName = this.dgrd_Popedom[this.dgrd_Popedom.CurrentCell.RowNumber,0].ToString().Trim();
    string strPopedom = this.chkLst_Priority.SelectedItem.ToString();
    string tempSendStrSQL = "delete from 权限清单 where (用户编号 = '" + strUserName +"'" +
    " and 权限名称 = '" + strPopedom + "')";
    this.link.UpdateDataBase(tempSendStrSQL);
    }
    catch
    {
    MessageBox.Show("数据库中的权限修改出错!","信息");
    }
    }
    }
    } private void dgrd_Popedom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (this.dgrd_Popedom.CurrentCell.RowNumber >= this.ds.Tables[0].Rows.Count)//防止出现所选的用户不在数据表中的情况
    {
    return;
    }
    this.chkLst_Priority.Enabled = true;
    blInitial = true; //控制当【权限管理】窗体刚生成时和点击其他用户时使CheckedListBox控件中数据发生改变时不响应ItemCheck事件
    //以下代码实现的是当dataGrid中所选用户改变时,根据选中用户权限重新设置listBox的功能
    for (int i=0;i<this.chkLst_Priority.Items.Count;i++)//将listBox中所有权限设为未选中
    {
    this.chkLst_Priority.SetItemChecked(i,false);
    }
    int intRowNumber = this.dgrd_Popedom.CurrentCell.RowNumber;
    string selectedUserID = this.ds.Tables[0].Rows[intRowNumber][0].ToString();
    string tempStrSQL = "select 权限名称 from 权限清单 " + " where 用户编号 = '" + selectedUserID + "'";
    popedomDataTable = link.SelectDataBase(tempStrSQL);//查询所选用户权限
    for (int i=0;i<popedomDataTable.Rows.Count;i++)//重新设置listBox
    {
    for (int j=0;j<this.chkLst_Priority.Items.Count;j++)
    {
    if (this.chkLst_Priority.Items[j].ToString().Trim() == popedomDataTable.Rows[i][0].ToString().Trim())
    {
    this.chkLst_Priority.SetItemChecked(j,true);
    }
    }
    }
    blInitial = false;
    }
      

  2.   

    数据库的User表中加一用户类型列
    每次登录时,根据用户ID、密码、用户类别一起核对