如何从数据库中扣除整数值? [英] How can I deduct integer value from database?
问题描述
这是我的数据表名称CreditCard
CardID数字(18,0)未选中
cardName nvarchar(50)未选中>
余额int已检查
Email_Address nvarchar(50)未选中
这是我的存储过程
This is my Datatable name CreditCard
CardID numeric(18, 0) Unchecked
cardName nvarchar(50) Unchecked
balance int Checked
Email_Address nvarchar(50) Unchecked
This is my Stored Procedured
ALTER PROCEDURE dbo.FindCreditCard
AS
SELECT * FROM CreditCard
RETURN
ALTER PROCEDURE dbo.CreditDeduct
@CardID numeric(18,0),
@balance int
AS
UPDATE CreditCard SET CardID = @CardID WHERE balance =@balance
RETURN
这是我的班级。 />
This is my Class.
public void FindCredit(decimal ccnum, int bal) //Gets balance after clicking Checkout button
{
db_connection.Open();
int deductedCredit = 0;
SqlCommand sc = new SqlCommand("FindCreditCard", db_connection);
sc.CommandType = CommandType.StoredProcedure;
SqlDataReader sdr = sc.ExecuteReader();
while (sdr.Read())
{
if (Convert.ToDecimal(sdr["CardID"].ToString()) == ccnum)
{
deductedCredit = Convert.ToInt32(sdr["balance"]) - bal;
}
}
db_connection.Close();
deductCredit(ccnum, deductedCredit);
}
public void deductCredit(decimal ccnum, int bals) //Deducts balance at items table after clicking checkout button
{
db_connection.Open();
SqlCommand sc = new SqlCommand("CreditDeduct", db_connection);
sc.CommandType = CommandType.StoredProcedure;
sc.Parameters.Add("@CardID", SqlDbType.Decimal).Value = ccnum;
sc.Parameters.Add("@balance", SqlDbType.Int).Value = bals;
sc.ExecuteNonQuery();
db_connection.Close();
}
这是按钮。
and this is the button.
protected void imgbtnSubmit_Click1(object sender, ImageClickEventArgs e)
{
decimal ccnum;
int bal;
ccnum = Convert.ToDecimal(txtCCardNum.Text);
bal = Convert.ToInt32(tbxDeposit.Text);
myData.FindCredit(ccnum, bal);
}
帮帮我们我能' t扣除我的数据表CreditCard中的余额,或者如果您只是关于如何从数据库表中扣除值的另一个想法。感谢您的回答。
Help me guys I can't deduct the balance in my datatable CreditCard or if you just a other idea on how to deduct value from database table. Thank for your answer.
推荐答案
更改存储过程中的更新语句CreditDeduct来自
Change your update statement in your stored procedure CreditDeduct from
UPDATE CreditCard SET CardID = @CardID WHERE balance =@balance
要
To
UPDATE CreditCard SET balance =@balance WHERE CardID = @CardID
希望上述声明有助于解决您的问题。
Hope the above statement helps in solving your problem.
您的程序名称是CreditDeduct
但它实际上已设置使用参数提供的新余额。
在我看来你应该修改程序到
The name of your procedure isCreditDeduct
but it actually sets a new balance which is provided using parameter.
In my opinion you should modify the procedure to
ALTER PROCEDURE dbo.CreditDeduct
@CardID numeric(18,0),
@deduction int
AS
UPDATE CreditCard SET balance = balance - @deduction WHERE CardID = @CardID
RETURN
这样,余额将始终根据余额的当前情况设置为正确的值。现在,您有两个程序读取余额并根据有关余额的旧信息对其进行修改的风险。问题本身称为丢失更新。
有关详细信息,请参阅并发控制 [ ^ ]
这篇关于如何从数据库中扣除整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!