string userid = string.Empty;
string roseid = string.Empty;
SqlConnection connect = new SqlConnection("server=192.168.0.1;uid=sa;pwd=sa;database=data1");
connect.Open();
//获取userid
SqlCommand ComUserid = new SqlCommand("select userid from aspnet_Users where username='" + name + "'", connect);
SqlDataReader dr = ComUserid.ExecuteReader();
if (dr.Read())
{
userid = dr["userid"].ToString();
}
dr.Close();
dr.Dispose();
ComUserid.Dispose();
SqlCommand ComDelete = new SqlCommand("delete from [aspnet_UsersInRoles] where userid = '" + userid + "'", connect);
ComDelete.ExecuteNonQuery();//删除aspnet_UsersInRoles表中所有此用户得所有角色.
ComDelete.Dispose();
try
{
//获取选择得rolesid,更新角色.
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
string rosename = CheckBoxList1.Items[i].Text.ToString();
SqlCommand ComRosid = new SqlCommand("select roleid from aspnet_Roles where rolename='" + rosename + "'", connect);
SqlDataReader drRid = ComRosid.ExecuteReader();
if (drRid.Read())
{
roseid = drRid["roleid"].ToString();
}
drRid.Close();
drRid.Dispose();
ComRosid.Dispose();
string aa = "insert into [aspnet_UsersInRoles] [userid],[roleid] values('" + userid + "','" + roseid + "')";
SqlCommand cominsert = new SqlCommand("insert into [aspnet_UsersInRoles] ([userid],[roleid]) values('" + userid + "','" + roseid + "')", connect);
cominsert.ExecuteNonQuery();
cominsert.Dispose();
}
}
connect.Close();//关闭数据库.
connect.Dispose();
}
catch (Exception ex)
{
throw ex;
}