C# 更新数据库中的布尔值

C# 更新数据库中的布尔值,c#,asp.net,ms-access,C#,Asp.net,Ms Access,在我的asp.net网页上有一个按钮,我对该按钮进行了编码,以便在pageload上读取access数据库中布尔列的值,并且该按钮的作用会根据列的值是真是假而改变 本质上,此按钮是产品的显示/隐藏按钮(单击它以隐藏产品,或者如果已隐藏,则单击它以使其可见) 我得到了一些奇怪的行为,比如当产品被隐藏时,点击使产品可见可以工作(更新数据库),然而,隐藏它却不行,我无法理解为什么一个可以工作,而另一个不行 代码如下: if (!IsPostBack) try { s

在我的asp.net网页上有一个按钮,我对该按钮进行了编码,以便在pageload上读取access数据库中布尔列的值,并且该按钮的作用会根据列的值是真是假而改变

本质上,此按钮是产品的显示/隐藏按钮(单击它以隐藏产品,或者如果已隐藏,则单击它以使其可见)

我得到了一些奇怪的行为,比如当产品被隐藏时,点击使产品可见可以工作(更新数据库),然而,隐藏它却不行,我无法理解为什么一个可以工作,而另一个不行

代码如下:

 if (!IsPostBack)

    try
    {
        s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
        conn = new OleDbConnection(s);
        cmd = new OleDbCommand("SELECT * FROM products WHERE products.prod_id = @test", conn);
        OleDbParameter test = new OleDbParameter("@test", OleDbType.Integer);
        test.Value = Request.QueryString["prod_id"];
        cmd.Parameters.Add(test);

        conn.Open();
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        title.Text = dr["shortdesc"].ToString();
        description.Text = dr["longdesc"].ToString();
        price.Text = dr["price"].ToString();

        productcat = dr["cat"].ToString();
        product_live = dr["live"].ToString();


    }
    catch (Exception ex)
    {
        Response.Write(ex.Message.ToString());
    }
    finally
    {
        dr.Close();
        conn.Close();
    }

protected void Page_Load(object sender, EventArgs e)

if (!IsPostBack)
    {
        bool prod_live_bool = Convert.ToBoolean(product_live);

        if (prod_live_bool == true)
        {
            live_label.Text = "This product is visible to customers";
            livelabelbutton.Text = "Hide this product";
            livelabelbutton.Click += new EventHandler(this.hideproduct_click);

        }
        else
        {
            live_label.Text = "This product is not visible to customers";
            livelabelbutton.Text = "Make this product visible";
            livelabelbutton.Click += new EventHandler(this.showproduct_click);
        }

    }

 protected void hideproduct_click(object sender, EventArgs e)
{
    string prodid = Request.QueryString["prod_id"];
    s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
    string str = "UPDATE products SET live = @hide WHERE prod_id=@product";

    using (OleDbConnection conn = new OleDbConnection(s))
    {
        using (OleDbCommand cmd = new OleDbCommand(str, conn))
        {
            OleDbCommand mycommand = new OleDbCommand();

            OleDbParameter hideparam = new OleDbParameter("@hide", OleDbType.Boolean);
            hideparam.Value = false;
            cmd.Parameters.Add(hideparam);
            OleDbParameter product = new OleDbParameter("@product", OleDbType.VarChar);
            product.Value = prodid;
            cmd.Parameters.Add(product);

            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }
    Response.Redirect(Request.RawUrl);
}

protected void showproduct_click(object sender, EventArgs e)
{
    string prodid = Request.QueryString["prod_id"];

    s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
    string str = "UPDATE products SET live = @show WHERE prod_id=@product";

    using (OleDbConnection conn = new OleDbConnection(s))
    {
        using (OleDbCommand cmd = new OleDbCommand(str, conn))
        {
            OleDbCommand mycommand = new OleDbCommand();

            OleDbParameter hideparam = new OleDbParameter("@show", OleDbType.Boolean);
            hideparam.Value = true;
            cmd.Parameters.Add(hideparam);
            OleDbParameter product = new OleDbParameter("@product", OleDbType.VarChar);
            product.Value = prodid;
            cmd.Parameters.Add(product);

            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }
    Response.Redirect(Request.RawUrl);
}

抱歉,代码太长了

如果命令按钮的作用是切换
[live]
是/否字段的值,则让db引擎执行您需要的操作

updateproducts SET live=(非live)其中prod\u id=@product
Not
返回布尔值的倒数。因此
Not-True
返回
False
Not-False
返回
True
。因此,
UPDATE
语句在执行
UPDATE
之前,将
live
设置为其包含的内容的倒数。在Access会话中将其作为新查询进行尝试,以检查其操作方式


如果这是令人满意的,那么隐藏和显示就不需要单独的例程。

OMG。。。。代码隐藏中的sql语句字符串。。多好的编程模式啊,那么糟糕吗?我对ASP.NET很陌生…这取决于你认为什么不好…对我来说,将整个应用程序放在一个代码文件中,没有分离的关注点,没有数据访问层,甚至没有ORM,这是不好的。没错,它只是用来切换“live”列中yes/no字段的值。你能再解释一下你的建议是如何起作用的吗?我试着实现它,但它不起作用…汉森你是一个天才,这非常有效,非常感谢你的帮助@HansUP
+1
对于该
字符串,该字符串不能包含null
对话。。从未有机会表示感谢:)