得到错误的返回,SQL - if

发布于 2024-11-24 06:03:58 字数 1283 浏览 1 评论 0原文

我有下一个方法:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩耳倾听 2024-12-01 06:03:58

我认为可能的问题是上述 SQL 的结果可能返回超过 1 行。因此,它会给出错误的结果。

尝试将代码更改为:

string commandtext = "SELECT TOP 1 Minbuy FROM items WHERE main = 1";
string commandtext2 = "SELECT TOP 1 ordersTillNow FROM items WHERE main = 1";

您可以使用一个选择,而不是 2 个选择

string queryString = "SELECT Minbuy, ordersTillsNow FROM items WHERE main = 1";
using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            MinBuy = int.Parse(reader[0].ToString());
            ordersTillsNow = int.Parse(reader[1].ToString());
            //Console.WriteLine(String.Format("{0}", reader[0]));
        }
    }

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 :

string commandtext = "SELECT TOP 1 Minbuy FROM items WHERE main = 1";
string commandtext2 = "SELECT TOP 1 ordersTillNow FROM items WHERE main = 1";

Instead of having 2 selects you can use one select

string queryString = "SELECT Minbuy, ordersTillsNow FROM items WHERE main = 1";
using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            MinBuy = int.Parse(reader[0].ToString());
            ordersTillsNow = int.Parse(reader[1].ToString());
            //Console.WriteLine(String.Format("{0}", reader[0]));
        }
    }
妄司 2024-12-01 06:03:58

也许您需要:

if (bla())
{
    Image_V.Visible = true;
    Image_X.Visible = false;
}
else
{
   Image_V.Visible = false;
   Image_X.Visible = true;
}

Perhaps you need:

if (bla())
{
    Image_V.Visible = true;
    Image_X.Visible = false;
}
else
{
   Image_V.Visible = false;
   Image_X.Visible = true;
}
别念他 2024-12-01 06:03:58

好吧,让我们只访问数据库一次,让它回答我们的问题:

private bool bla()
{
    using(SqlConnection connection = new SqlConnection("Data Source=****;Initial Catalog=****;User ID=****;Password=****;Integrated Security=False;"))
    {
        string commandtext = "SELECT CONVERT(bit,CASE WHEN Minbuy < ordersTillNow THEN 1 ELSE 0 END) FROM items WHERE main = 1";
        SqlCommand command = new SqlCommand(commandtext, connection);
        connection.Open();
        return (bool)command.ExecuteScalar();    
    }
}

此代码还确保关闭连接,这是您之前没有处理过的。但是,如果您仍然没有看到预期的结果,那么正如其他人所建议的那样,您需要检查图像显示代码是否按预期工作。也许有:

Image_V.Visible = bla();
Image_X.Visible = !Image_V.Visible;

相反。

Well, let's hit the database only once, and get it to answer the question for us:

private bool bla()
{
    using(SqlConnection connection = new SqlConnection("Data Source=****;Initial Catalog=****;User ID=****;Password=****;Integrated Security=False;"))
    {
        string commandtext = "SELECT CONVERT(bit,CASE WHEN Minbuy < ordersTillNow THEN 1 ELSE 0 END) FROM items WHERE main = 1";
        SqlCommand command = new SqlCommand(commandtext, connection);
        connection.Open();
        return (bool)command.ExecuteScalar();    
    }
}

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:

Image_V.Visible = bla();
Image_X.Visible = !Image_V.Visible;

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文