SQL命令不起作用 [英] SQL command does not work

查看:109
本文介绍了SQL命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好帅哥

我有此代码
它不会说库存不足"
我不明白哪里出了问题.
请帮助
我正在使用c#

Hello dudes

I have this code
It does not say ''There are not enough items in stock''
I cant understood where is problem.
please help
Im using c#

SqlCommand cmd1 = new SqlCommand("IF (Select UnitsInStock from Products WHERE BarCode=@BarCode) > @Quantity BEGIN UPDATE Products SET UnitsInStock=(UnitsInStock-@Quantity) WHERE BarCode=@BarCode END ELSE BEGIN PRINT ''There are not enough items in stock''END", new SqlConnection(Program.ConnectionString));
            cmd1.Connection.Open();
            for (int i = 0; i < dtbProducts.Rows.Count; i++)
            {
                cmd1.Parameters.AddWithValue("Quantity", (int)dtbProducts.Rows[i]["Quantity"]);
                cmd1.Parameters.AddWithValue("BarCode", dtbProducts.Rows[i]["BarCode"].ToString());
                cmd1.ExecuteNonQuery();
                cmd1.Parameters.Clear();
            }

            cmd1.Connection.Close();
            cmd.Connection.Close();
            this.Close();

        }

推荐答案

1.您应该将存储过程用于这种操作,而不是直接查询.

2.在查询中,其他部分显示库存不足".您是否已根据数量和条形码值检查了If的哪一部分正在执行?

3.第一步应该是直接在SQL中运行定义的查询,并查看给定几个参数的结果.一旦工作,然后在查询中使用它.

4.您可以循环执行命令,而无需存储返回的结果集.我怀疑这是故意的!当前,您的代码仅显示最后一次迭代的结果.格式化和微调您的代码,检查并存储所需的值.使用VS DEBUGGER,会有所帮助!
1. You should have used Stored Procedure for this kind of operation instead of a direct query.

2. In your query, the else part says ''There are not enough items in stock''. Have you checked that based on the quantity and barcode value, which part of If is executing?

3. Your first step should be to run the defined query directly in SQL and see the results for given few parameters. Once that is working then use it in your query.

4. You execute your command in a loop without storing the result set returned in anything. I doubt that is intended! Currently, your code just shows you what is the result for the last iteration. Format and fine tune your code, check and store needed values. Use VS DEBUGGER, it will help!


使用以下语句
选择库存不足"
Use following statement
SELECT ''There are not enough items in stock''


这篇关于SQL命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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