我正在处理库存管理系统项目,我正在使用c#
和mssql
,我遇到了一个问题,我在数量列上定义了一个检查约束,即(QUANTITY>=0)
,但它是不工作。我在C#编写了以下查询:
INSERT INTO SALES
VALUES(@ITEM_MODEL
,@QUANTITY
,@AMOUNT
,@DATE_SALES
,''
,@TOTAL_PRICE
);
UPDATE SALES
SET PROFIT=TOTAL_PRICE-(SELECT RATE
FROM ITEM_DETAILS
WHERE ITEM_MODEL=@ITEM_MODEL
)
WHERE ITEM_MODEL=@ITEM_MODEL;
UPDATE ITEM_DETAILS SET QUANTITY=QUANTITY-(SELECT QUANTITY
FROM SALES
WHERE ITEM_MODEL=@ITEM_MODEL
)
WHERE ITEM_MODEL=@ITEM_MODEL;
UPDATE ITEM_DETAILS SET AMOUNT = AMOUNT - (
(SELECT RATE
FROM ITEM_DETAILS
WHERE ITEM_MODEL=@ITEM_MODEL
) *
(SELECT QUANTITY
FROM SALES
WHERE ITEM_MODEL=@ITEM_MODEL
)
)
where ITEM_MODEL=@ITEM_MODEL;
另一个问题是,我在try-catch
块中执行它以处理quantity<0
的异常,但是当我运行它时,它总是进入catch
块并显示该数量是{0}而actual quantity is greater than zero
。
以下是执行查询的try-catch块:
try
{
SqlConnection con = new SqlConnection("Data Source=Nadeem-PC;Initial Catalog=MyDatabase;Integrated Security=TRUE");
da.InsertCommand = new SqlCommand("INSERT INTO ITEM_DETAILS VALUES(@ITEM_MODEL,@ITEM_NAME,@ITEM_DESCRIPTION,@VENDOR_NAME,@QUANTITY,@RATE,@AMOUNT,@INVOICE_NUM,@DATE)", con);
da.InsertCommand.Parameters.Add("@ITEM_MODEL", SqlDbType.VarChar).Value = txtItemModel.Text;
da.InsertCommand.Parameters.Add("@ITEM_NAME", SqlDbType.VarChar).Value = txtItemName.Text;
da.InsertCommand.Parameters.Add("@ITEM_DESCRIPTION", SqlDbType.VarChar).Value = txtItemDescription.Text;
da.InsertCommand.Parameters.Add("@VENDOR_NAME", SqlDbType.VarChar).Value = txtVendor.Text;
da.InsertCommand.Parameters.Add("@QUANTITY", SqlDbType.Int).Value = txtQuantity.Text;
da.InsertCommand.Parameters.Add("@RATE", SqlDbType.Money).Value = txtRate.Text;
da.InsertCommand.Parameters.Add("@AMOUNT", SqlDbType.Money).Value = txtAmount.Text;
da.InsertCommand.Parameters.Add("@INVOICE_NUM", SqlDbType.Int).Value = txtInvoice.Text;
da.InsertCommand.Parameters.Add("@DATE", SqlDbType.Date).Value = dateTimePicker1.Value;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();
}
catch
{
MessageBox.Show("Something wrong enters", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}