数量控制在C# [英] Qty Control in C#
问题描述
pstrjds,从你的代码提供我想实现的代码和MSDN链接你以前提供的几天。我不知道如何把它在一起,但我看到一条消息,变量名'@ID'已声明。变量名必须是查询批处理或存储procedurce中是唯一的。我需要创建一个存储procedurce?我可以承担我的ID位权为好,在那里你解释的次数?谢谢。
在此输入代码
cs.Open();
INT remainingStock = 0;
字符串查询=选择StockA SET QTY = @QTY+WHERE ID = @ID;;
的SqlCommand CMD =新的SqlCommand(查询,CS);
cmd.Parameters.Add(@ ID,SqlDbType.Int);
cmd.Parameters [@ ID]值= 1。
cmd.Parameters.AddWithValue(@ ID,查询);
试
{
如果(remainingStock == 1)
{
lbqty.Text =没有足够的库存。
}
,否则
{
cmd.CommandText =UPDATE StockA SET QTY = QTY-1
WHERE ID = 1;
INT rowsUpdated = cmd.ExecuteNonQuery();
remainingStock--;剩余=剩余股票:
+字符串
remainingStock.ToString();
txQty.Text =剩余;
lbqty.Text =剩余;
DGA.Update(); //这是DataGridView的
}(
的Int32的RowsAffected = cmd.ExecuteNonQuery);
Console.WriteLine(的RowsAffected:{0}的RowsAffected);
}
赶上(异常前)
{
MessageBox.Show(ex.Message);
}
cs.Close();
}
阅读后你的问题再一次,我更加坚信,我张贴的评论实际上是这个问题。你在你的问题时说,你的量不下去的时候。如果你看一下从行的返回值:
cmd.ExecuteNonQuery();
在您else语句,我相信你会得到一个0值,这表明没有记录更新的地方。我相信这个问题恰好与您的查询。如果你有一个实用工具,如LINQPad或SQL Management Studio连接到数据库并运行你已经在你的代码中列出的查询,我认为你会发现它没有更新任何东西。我强烈怀疑你的库存没有被存储在一个名为表 tblContacts
。 。这就是为什么你不能从中选择一个数量也不将其更新量
编辑:
意味着最初提到这一点,然后得到了牵制和忘了添加。我会把你的SqlCommand在using语句。 SqlCommand对象是一次性的,所以它是很好的做法,把一次性物品的使用语句或某种形式的try /终于模式在那里他们可以清理的。
其他编辑 - 调整你的代码:
cs.Open();
INT remainingStock = 0;
字符串查询=从tblInventory WHERE ID = 19选择数量;
使用(CMD的SqlCommand =新的SqlCommand(查询,CS))
{
VAR的结果= cmd.ExecuteScaler();
如果(结果!= NULL)
{
字符串str = result.ToString(); (!string.isNullOrWhiteSpace(STR))
如果
{
//注意:它可能会更安全int.TryParse在这里使用
remainingStock = Convert.ToInt32(CMD) ;
}
如果(remainingStock == 0)
{
lbqty.Text =没有足够的库存。
}
,否则
{
cmd.CommandText =UPDATE tblInventory SET QTY = QTY-1 WHERE ID = 19;
INT rowsUpdated = cmd.ExecuteNonQuery();
remainingStock--;
如果(remainingStock == 1)
{
lbqty.Text =重新排序其余个股:1。
}
,否则
{
串剩余=其余个股:+ remainingCount.ToString();
txtQty.Text =剩余;
lbqty.Text =剩余;
}
}
}
,否则
lbqty.Text =没有库存接触;
}
dgUpdate();
}
cs.Close();
pstrjds, From the code you have provided I am trying to implement that code and the msdn link you provided couple of days ago. I am not sure how to put it together but I am see a message that "The variable name '@ID' has already been declared. Variable names must be unique within a query batch or stored procedurce." Do I need to create a stored procedurce? Can I assume I got the ID bit right as well, where you explained number of times? Thank you. enter code here
cs.Open();
int remainingStock = 0;
string Query = "SELECT StockA SET QTY = @QTY " + "WHERE ID = @ID;";
SqlCommand cmd = new SqlCommand(Query, cs);
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@ID"].Value = 1;
cmd.Parameters.AddWithValue("@ID", Query);
try
{
if (remainingStock == 1)
{
lbqty.Text = "Not enough stocks.";
}
else
{
cmd.CommandText = "UPDATE StockA SET QTY = QTY-1
WHERE ID=1";
int rowsUpdated = cmd.ExecuteNonQuery();
remainingStock--;
string remaining = "Remaining stocks: " +
remainingStock.ToString();
txQty.Text = remaining;
lbqty.Text = remaining;
DGA.Update(); //this is DataGridView
}
Int32 rowsAffected = cmd.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
cs.Close();
}
After reading your question again, I am more convinced that the comment I posted is actually the problem. You said in your question that your quantity is not going down. If you look at the return value from the line:
cmd.ExecuteNonQuery();
in your else statement I believe you will get a value of 0. This indicates that no records where updated. I believe the problem is exactly related to your query. If you connect to your database with a utility such as LINQPad or the Sql Management studio and run the query you have listed in your code I think you will find it is not updating anything. I highly suspect that your inventory is not being stored in a table called tblContacts
. Which is why you can't select a quantity from it nor update a quantity in it.
Edit: Meant to mention this initially and then got sidetracked and forgot to add it. I would put your SqlCommand in a using statement. The SqlCommand object is disposable and so it is good practice to place disposable objects in a using statement or in some sort of try/finally pattern where they can be cleaned up.
Additional edit - restructuring your code:
cs.Open();
int remainingStock = 0;
string Query = "SELECT QTY from tblInventory WHERE ID=19";
using(SqlCommand cmd = new SqlCommand(Query, cs))
{
var result = cmd.ExecuteScaler();
if (result != null)
{
string str = result.ToString();
if (!string.isNullOrWhiteSpace(str))
{
// NOTE: It would probably be safer to use int.TryParse here
remainingStock = Convert.ToInt32(cmd);
}
if (remainingStock == 0)
{
lbqty.Text = "Not enough stocks.";
}
else
{
cmd.CommandText = "UPDATE tblInventory SET QTY = QTY-1 WHERE ID=19";
int rowsUpdated = cmd.ExecuteNonQuery();
remainingStock--;
if (remainingStock == 1)
{
lbqty.Text = "Re-order. Remaining stocks: 1";
}
else
{
string remaining = "Remaining stocks: " + remainingCount.ToString();
txtQty.Text = remaining;
lbqty.Text = remaining;
}
}
}
else
lbqty.Text = "No stock for contact";
}
dgUpdate();
}
cs.Close();
这篇关于数量控制在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!