1、 添加用户群组权限:用户key入AD账号,判定是否属于USER表(logonID),是的话根据用户选择的GROUP(下拉框,来源于GROUP表),新增USERID,GROUPID到GROUP表中
2、 查询:根据GROUP或者是LogonID查询到相关用户的群组权限,并有删除功能
要求:
1、 控件、参数命名规范,命名规则http://www.cnblogs.com/muou/archive/2009/06/16/1504059.html
2、 DB连接参数需要配置在WEB.CONFIG中,DB操作使用存储过程实现(增、删)
public partial class inset : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
gvList.DataBind();
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
string strCon = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString();
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strSql = "select * from[USER] where [LOGONID]='"+txtAD.Text.Trim()+"'";
SqlCommand com = new SqlCommand(strSql,con);
SqlDataReader dr = com.ExecuteReader();
if(dr.Read())
{
string strconn = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
lblName.Text = dr["USERNAME"].ToString();
lblName.Visible = true;
string strSqlInsert = @"insert into [GROUPUSER](USERID,GROUPID) select [USER].USERID , [GROUP].GROUPID from [USER],[GROUP]
where GROUPNAME='" + ddlGroup.SelectedValue + "' and USERNAME='" + lblName.Text + "'";
//string strSqlInsert = @"insert into [GROUPUSER](USERID,GROUPID) select * where not exists (select [USER].USERID , [GROUP].GROUPID from [USER],[GROUP] where GROUPNAME='" + ddlGroup.SelectedValue + "' and USERNAME='" + lblName.Text + "')";
SqlCommand comm = new SqlCommand(strSqlInsert, conn);
comm.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('添加权限成功!');</script>");
}
else
{
lblName.Text = "不存在此AD!";
lblName.Visible = true;
btnOk.Enabled = false;
}
con.Close();
}
public static SqlConnection Conn()
{
string strconn = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString();
SqlConnection con = new SqlConnection(strconn);
con.Open();
return con;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (txtLogonId.Text.Length <= 0)
{
DataSet ds = new DataSet();
string strSqlSearch = @"select LOGONID,USERNAME,GROUPNAME from [USER] left outer join [GROUPUSER]
on [USER].USERID=[GROUPUSER].USERID right outer join [GROUP] on [GROUP].GROUPID=[GROUPUSER].GROUPID where GROUPNAME='" + ddlSearch.SelectedValue + "'";
//string strSqlSearch = @"select LOGONID,USERNAME,GROUPNAME from [USER],[GROUP] where GROUPNAME='" + ddlSearch.SelectedValue + "'";
SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSqlSearch, Conn());
sqlAdapter.Fill(ds, "Result");
//m_DataView = ds.Tables[0].DefaultView;
Conn().Close();
gvList.DataSource = ds.Tables["Result"].DefaultView;
//gvList.DataSource = m_DataView;
gvList.DataBind();
sqlAdapter.Dispose();
}
else if (txtLogonId.Text.Length>0)
{
string strLogon = @"select LOGONID,USERNAME,GROUPNAME from [GROUP] left join [GROUPUSER]
on [GROUP].GROUPID=[GROUPUSER].GROUPID right join [USER] on [GROUPUSER].USERID=[USER].USERID where LOGONID='"+txtLogonId.Text+"' ";
SqlDataAdapter sqlApt = new SqlDataAdapter(strLogon,Conn());
DataSet ds1 = new DataSet();
sqlApt.Fill(ds1,"Result1");
//m_DataView = ds1.Tables[0].DefaultView;
Conn().Close();
gvList.DataSource = ds1.Tables["Result1"].DefaultView;
//gvList.DataSource = m_DataView;
gvList.DataBind();
sqlApt.Dispose();
}
}
private DataView m_DataView;
protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//执行删除
string strDelet = @"delete from [GROUPUSER] WHERE USERID in( select USERID from [USER] where LOGONID='"+gvList.DataKeys[e.RowIndex].Value.ToString()+"')";
SqlCommand sqlcom = new SqlCommand(strDelet,Conn());
sqlcom.ExecuteNonQuery();
//Conn().Close();
//this.gvList.DataBind();
BindData();
Response.Write("<script>alert('删除成功!')</script>");
}
protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvList.PageIndex = e.NewPageIndex;
gvList.DataSource = m_DataView;
//gvList.DataBind();
BindData();
}
private void BindData()
{
//将数据部署到GridView中,
string sqlStr = @"select LOGONID,USERNAME,GROUPNAME from [USER] left join [GROUPUSER]
on [USER].USERID=[GROUPUSER].USERID right join [GROUP] on [GROUP].GROUPID=[GROUPUSER].GROUPID where GROUPNAME='" + ddlSearch.SelectedValue + "'";
SqlDataAdapter dad = new SqlDataAdapter(sqlStr, Conn());
DataSet dst = new DataSet();
dad.Fill(dst);
gvList.DataSource = dst;
gvList.DataBind();
}
protected void txtAD_TextChanged1(object sender, EventArgs e)
{
this.btnOk.Enabled = !string.IsNullOrWhiteSpace(this.txtAD.Text);
}
}