单个表的多行
我有一张有 3 列的桌子。 viz id,profile_id,plugin_id
。现在可以有超过 1 个与单个配置文件关联的插件,现在我如何从数据库中获取与 profile_id 关联的所有插件,该 profile_id 来自于在登录页面 当我尝试应用相同的查询时,它返回带有最后一条记录的plugin_id的数据 查询如下
SqlCommand cmd1 = new SqlCommand(
"select plugin_id from profiles_plugins where profile_id=" +
Convert.ToInt32(Session["cod"]), con);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
while (dr1.Read())
{
Session["edp1"] = Convert.ToInt32(dr1[0]);
}
}
dr1.Close();
cmd1.Dispose();
i am having a table with 3 col. viz id,profile_id,plugin_id
.there can be more than 1 plugins associated with a single profile now how can i fetch from the database all the plugins associated with a profile_id which comes from the session variable defined in the login page
when I try to apply the query for the same it returns the data with the plugin_id of the last record
the query is as follows
SqlCommand cmd1 = new SqlCommand(
"select plugin_id from profiles_plugins where profile_id=" +
Convert.ToInt32(Session["cod"]), con);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
while (dr1.Read())
{
Session["edp1"] = Convert.ToInt32(dr1[0]);
}
}
dr1.Close();
cmd1.Dispose();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个“错误”,因为您每次在 while 循环期间分配给同一个变量,这就是为什么看起来您只得到最后一行!
there is an "error" because you assign during your while loop everytime to the same variable, thats why it looks like you get only the last row!
我建议您编写一个单独的函数来从数据库中检索值。另外,您应该使用参数化查询来避免 SQL 注入:
然后像这样调用该函数:
I would recommend you writing a separate function for retrieving values from the database. Also you should use parametrized queries to avoid SQL injection:
and then call the function like this:
我想您想保存会话中的所有值,具体方法如下:
当您从会话中读取数据时,您只需键入:
但您确实应该重构代码,代码不应该管理数据访问和会话处理在同一个地方。
I guess you want to save all the values in the session and here is how you do it:
And when you read from the session you just type:
But you should really refactor your code, the code shouldn't manage data access and session handling in the same place.