【问题标题】:code asp.net c# oledb, cookies, if else代码 asp.net c# oledb, cookies, if else
【发布时间】:2015-06-12 17:24:35
【问题描述】:

有人可以帮助理解这段代码吗?

protected void Page_Load(object sender, EventArgs e)
    {
        Database database = new Database();

        OleDbConnection conn = database.connectDatabase();

        if (Request.Cookies["BesteldeArtikelen"] == null)
        {
            lbl_leeg.Text = "Er zijn nog geen bestelde artikelen";
        }
        else
        {
            HttpCookie best = Request.Cookies["BesteldeArtikelen"];

            int aantal_bestel = best.Values.AllKeys.Length;
            int[] bestelde = new int[aantal_bestel];
            int index = 0;

            foreach (string art_id in best.Values.AllKeys) 
            {
                int aantalbesteld = int.Parse(aantalVoorArtikel(int.Parse(art_id)));

                int artikel_id = int.Parse(art_id); // moet getalletje zijn

                if (aantalbesteld != 0)  
                {
                    bestelde[index] = artikel_id;
                }
                index++;
            }

            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT artikel_id, naam, prijs, vegetarische FROM artikel WHERE artikel_id IN (" +
                String.Join(", ", bestelde) + ")";


            try
            {
                conn.Open();

                OleDbDataReader reader = cmd.ExecuteReader();
                GridView1.DataSource = reader;
                GridView1.DataBind();
            }

            catch (Exception error)
            {
                errorMessage.Text = error.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

还有这部分代码我不太明白:

public string aantalVoorArtikel(object id)
    {
        int artikel_id = (int)id;

        if (Request.Cookies["BesteldeArtikelen"] != null &&
            Request.Cookies["BesteldeArtikelen"][artikel_id.ToString()] != null)
        {
            return Request.Cookies["BesteldeArtikelen"][artikel_id.ToString()];
        }
        else
        {
            return "0";
        }
    }

【问题讨论】:

  • 那么您是遇到问题了,还是希望有人解释代码在做什么?
  • 关于代码的更多解释,我会很感激最后一点代码的答案。我无法掌握它..
  • 欢迎来到 Stack Overflow!有一本关于 C# 编程语言的完整手册。在询问有关语言语法的基本问题之前,您应该先查看它。见C# Programming Guide。详细参考请见C# Reference

标签: c# asp.net if-statement cookies oledb


【解决方案1】:

它从 cookie 中提取值并构建一个 int 数组。 (如果 cookie 值为 null,则显示一条消息)然后在查询数据库时将 int 数组用作 SQL IN 运算符的值。然后将结果集绑定到 GridView。

【讨论】:

    猜你喜欢
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多