更新数据库中的布尔值
本文关键字:布尔值 数据库 更新 | 更新日期: 2023-09-27 17:54:27
我有一个按钮在我的asp.net网页上,我已经编码读取在页面加载访问数据库中的布尔列的值,什么按钮做根据列的值是否为真或假的变化。
本质上这个按钮是一个产品的显示/隐藏按钮(点击它隐藏产品,或者如果已经隐藏,点击它使其可见)。
我得到一些奇怪的行为,当产品被隐藏,点击使产品可见的作品(更新数据库),然而,隐藏它不,我不能弄清楚为什么一个会工作,但另一个不会。
代码如下:
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]
Yes/No字段的值,让db引擎做你需要的。
UPDATE products SET live = (Not live) WHERE prod_id=@product
Not
返回布尔值的倒数。所以Not True
返回False
, Not False
返回True
。因此,UPDATE
语句在执行UPDATE
之前将live
设置为它所包含的内容的倒数。在Access会话中作为一个新查询尝试一下,看看它是如何操作的。
如果满意,则不需要单独的hide和show例程。