单个表的多行

发布于 2024-10-10 18:07:40 字数 569 浏览 1 评论 0原文

我有一张有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂归处 2024-10-17 18:07:40

有一个“错误”,因为您每次在 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!

走过海棠暮 2024-10-17 18:07:40

我建议您编写一个单独的函数来从数据库中检索值。另外,您应该使用参数化查询来避免 SQL 注入:

public IEnumerable<int> GetPluginIds(int profileId)
{
    using (var connection = new SqlConnection("SOME CONNECTION STRING"))
    using (var cmd = connection.CreateCommand())
    {
        connection.Open();
        cmd.CommandText = "SELECT plugin_id FROM profiles_plugins WHERE profile_id = @profile_id";
        cmd.Parameters.AddWithValue("@profile_id", profileId);
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return reader.GetInt32(0);
            }
        }
    }
}

然后像这样调用该函数:

// get the profile_id from session
int profileId = Convert.ToInt32(Session["cod"]);

// fetch associated plugin ids
int[] pluginIds = GetPluginIds(profileId).ToArray();

// store the resulting array into session
Session["pluginIds"] = pluginIds;

I would recommend you writing a separate function for retrieving values from the database. Also you should use parametrized queries to avoid SQL injection:

public IEnumerable<int> GetPluginIds(int profileId)
{
    using (var connection = new SqlConnection("SOME CONNECTION STRING"))
    using (var cmd = connection.CreateCommand())
    {
        connection.Open();
        cmd.CommandText = "SELECT plugin_id FROM profiles_plugins WHERE profile_id = @profile_id";
        cmd.Parameters.AddWithValue("@profile_id", profileId);
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return reader.GetInt32(0);
            }
        }
    }
}

and then call the function like this:

// get the profile_id from session
int profileId = Convert.ToInt32(Session["cod"]);

// fetch associated plugin ids
int[] pluginIds = GetPluginIds(profileId).ToArray();

// store the resulting array into session
Session["pluginIds"] = pluginIds;
我不咬妳我踢妳 2024-10-17 18:07:40

我想您想保存会话中的所有值,具体方法如下:

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con);

SqlDataReader dr1 = cmd1.ExecuteReader();
var yourList = new List<int>();
if (dr1.HasRows)
{
    while (dr1.Read())
    {
       yourList.Add(Convert.ToInt32(dr1[0]));

    }
}
Session["edp1"] = yourList;
dr1.Close();
cmd1.Dispose();

当您从会话中读取数据时,您只需键入:

var yourList = (List<int>)Session["edp1"];

但您确实应该重构代码,代码不应该管理数据访问和会话处理在同一个地方。

I guess you want to save all the values in the session and here is how you do it:

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con);

SqlDataReader dr1 = cmd1.ExecuteReader();
var yourList = new List<int>();
if (dr1.HasRows)
{
    while (dr1.Read())
    {
       yourList.Add(Convert.ToInt32(dr1[0]));

    }
}
Session["edp1"] = yourList;
dr1.Close();
cmd1.Dispose();

And when you read from the session you just type:

var yourList = (List<int>)Session["edp1"];

But you should really refactor your code, the code shouldn't manage data access and session handling in the same place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文