【发布时间】:2014-08-18 07:59:46
【问题描述】:
我在为启用了两个/三个不同区域的用户管理授权时遇到问题,例如,具有此配置文件的用户我没有问题:
User = Foo
Area = East
Level = 2
相反,对于用户个人资料:
User = Pluto
Area = East
Area = West
Level = 2
语句 Users() 根据成员区域和预期级别(0、1 和 2)对授权用户表执行访问控制,仅考虑西区用户冥王星,而不是同时启用东区和西区。
下面是我的代码。
任何帮助将不胜感激,在此先感谢。
protected void Users()
{
using (OdbcConnection conn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
sql = " SELECT * FROM ";
sql = sql + " tblUsers ";
sql = sql + " WHERE (Email = ? ";
sql = sql + " AND degree IS NOT NULL); ";
using (OdbcCommand command =
new OdbcCommand(sql, conn))
{
try
{
command.Parameters.AddWithValue("param1", Server.UrlDecode(Request.Cookies["email"].Value));
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
degree = reader["degree"].ToString();
area = reader["Area"].ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
}
编辑 1
string Level;
string Area;
public class GrantUser
{
public string Area { get; set; }
public string Level { get; set; }
public GrantUser() { }
public GrantUser(string Area, string Level)
{
this.Area = Area;
this.Level = Level;
}
}
protected void Users()
{
using (OdbcConnection conn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
sql = " SELECT * FROM tblUsers WHERE (Email = ? AND Level IS NOT NULL); ";
using (OdbcCommand command =
new OdbcCommand(sql, conn))
{
try
{
command.Parameters.AddWithValue("param1", Server.UrlDecode(Request.Cookies["email"].Value));
command.Connection.Open();
List<GrantUser> lsGrantUser = new List<GrantUser>();
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Level = reader["Level"].ToString();
Area = reader["Area"].ToString();
lsGrantUser.Add(new GrantUser(reader["Area"].ToString(), reader["Level"].ToString()));
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label area = (Label)e.Row.FindControl("Area");
if (!string.IsNullOrEmpty(Level.ToString()))
{
if (Level.ToString() == "0")
{
//here the condition 0
}
if (Level.ToString() == "1")
{
if (area.Text == Area.ToString())
{
//here the condition 1
}
}
if (Level.ToString() == "2")
{
if (area.Text == Area.ToString())
{
//here the condition 2
}
}
}
}
}
public DataTable GridViewBind()
{
sql = " SELECT ....... ; ";
try
{
dadapter = new OdbcDataAdapter(sql, conn);
dset = new DataSet();
dset.Clear();
dadapter.Fill(dset);
DataTable dt = dset.Tables[0];
GridView1.DataSource = dt;
conn.Open();
GridView1.DataBind();
if (dt.Rows.Count == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('No data.');", true);
}
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
dadapter.Dispose();
dadapter = null;
conn.Close();
}
}
编辑 2
Users();
GridView1.DataBind();
【问题讨论】:
-
不要
throw exstackoverflow.com/questions/730250/… 此外,command.Connection.Close()与using语句是多余的。实际上你的try-catch-finally是多余的。 -
degree和area声明在哪里?您应该创建一个类User至少具有四个属性:string Name、string Degree、List<string> Areas和int Level。重要的是列表。 -
感谢在公共部分课程中声明的学位和区域
-
请参阅我第一篇文章中的EDIT 1。输出不变。
-
你还在用一个
Level和一个Area,为什么?您已经初始化了一个本地List<GrantUser>,但从未使用它。