得到错误的返回,SQL - if
我有下一个方法:
private bool bla()
{
int Minbuy, ordersTillNow;
{
SqlConnection connection = new SqlConnection("Data Source=****;Initial Catalog=****;User ID=****;Password=****;Integrated Security=False;");
string commandtext = "SELECT Minbuy FROM items WHERE main = 1";
string commandtext2 = "SELECT ordersTillNow FROM items WHERE main = 1";
SqlCommand command = new SqlCommand(commandtext, connection);
SqlCommand command2 = new SqlCommand(commandtext2, connection);
connection.Open();
Minbuy = (int)command.ExecuteScalar();
ordersTillNow = (int)command2.ExecuteScalar();
if (Minbuy < ordersTillNow)
return true;
else
return false;
}
}
并且在page_load上使用该方法:
if (bla())
{
Image_V.Visible = true;
}
else
{
Image_X.Visible = true;
}
SQL中查询结果的值为:
最小购买量 = 5
订单截止日期 = 1
奇怪的是 - 不管数据库中的值是多少(我已将值更改为: MinBuy = 1 和ordersTillNow = 8) - 它显示 image_v 。 (在 aspx 页面上 - 两个图像可见都设置为 false)。
代码有什么问题?
表设计:
表名:items
列:itemId(int)、main(bit)、MinBuy(int)、ordersTillNow(int)。
I have the next method:
private bool bla()
{
int Minbuy, ordersTillNow;
{
SqlConnection connection = new SqlConnection("Data Source=****;Initial Catalog=****;User ID=****;Password=****;Integrated Security=False;");
string commandtext = "SELECT Minbuy FROM items WHERE main = 1";
string commandtext2 = "SELECT ordersTillNow FROM items WHERE main = 1";
SqlCommand command = new SqlCommand(commandtext, connection);
SqlCommand command2 = new SqlCommand(commandtext2, connection);
connection.Open();
Minbuy = (int)command.ExecuteScalar();
ordersTillNow = (int)command2.ExecuteScalar();
if (Minbuy < ordersTillNow)
return true;
else
return false;
}
}
And the use of the method on page_ load:
if (bla())
{
Image_V.Visible = true;
}
else
{
Image_X.Visible = true;
}
The values on the result in query in the SQL is:
MinBuy = 5
ordersTillNow = 1
The weird thing is - nevermind what is the value in the db (i've changed the value to: MinBuy = 1 and ordersTillNow = 8) - it's display the image_v . (on the aspx page - both of the image visble set to false).
What is wrong in the code ?
The table desgin:
table name: items
columns: itemId(int) , main (bit) , MinBuy (int) , ordersTillNow(int) .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为可能的问题是上述 SQL 的结果可能返回超过 1 行。因此,它会给出错误的结果。
尝试将代码更改为:
您可以使用一个选择,而不是 2 个选择
What I can think that might be the problem is that the result from the above SQL might return more than 1 row. Thus, it will give the wrong result.
Try changing the code to :
Instead of having 2 selects you can use one select
也许您需要:
Perhaps you need:
好吧,让我们只访问数据库一次,让它回答我们的问题:
此代码还确保关闭连接,这是您之前没有处理过的。但是,如果您仍然没有看到预期的结果,那么正如其他人所建议的那样,您需要检查图像显示代码是否按预期工作。也许有:
相反。
Well, let's hit the database only once, and get it to answer the question for us:
This code also makes sure to close the connection, that you were not previously taking care of. But if you're still not seeing the expected results, then as others have suggested, you need to check that the image display code is working as expected. Maybe have:
instead.