代码asp.net c#oledb,cookies,if else [英] code asp.net c# oledb, cookies, if else

查看:125
本文介绍了代码asp.net c#oledb,cookies,if else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can somebody help understand this code?

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";
        }
    }


推荐答案

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

It extracts values from a cookie and builds an int array. (Displays a message if the cookie value is null) The int array is then used as the value for the SQL IN operator when querying the database. The result set is then bound to the GridView.

这篇关于代码asp.net c#oledb,cookies,if else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆