protected void btnOk_Click1(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+"'";
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();
}
本文展示了一个使用C#和SQL Server进行用户权限添加的例子。该示例通过连接数据库、查询用户信息并为指定用户分配组权限来实现功能。代码中包含了数据库连接设置、SQL查询构造及执行等关键步骤。

被折叠的 条评论
为什么被折叠?



