try
{
sqlCommandWithdraw.Connection.Open();
sqlCommandWithdraw.Parameters["@cardNumber"].Value = Class1.cardNumber;
readdata = sqlCommandWithdraw.ExecuteReader();
while (readdata.Read())
{
balanceDB = decimal.Parse(readdata["balance"].ToString());
}
decimal withdrawAmm = Convert.ToDecimal(textWithdraw.Text);
balanceDB = balanceDB - withdrawAmm;
sqlCommandWithdraw.Connection.Close();
sqlCommandUpdate.Connection.Open();
sqlCommandUpdate.Parameters["@cardNumber"].Value = Class1.cardNumber;
sqlCommandUpdate.Parameters["@balanceDB"].Value = Class1.cardNumber;
readdata = sqlCommandUpdate.ExecuteReader();
MessageBox.Show(balanceDB +" Successfully Withdrawn");
}
我正在为自动取款机的代码工作我在退出时有点东西它看起来很好但似乎没有改变余额以反映数据库中的提款
我的命令就是这样(更新)
update dbo.Accounts
set balance = @balanceDB
from dbo.ATMCards
INNER JOIN dbo.Accounts ON dbo.ATMCards.accountID = dbo.Accounts.accountID
where (dbo.ATMCards.cardNumber = @cardNumber)
这是我选择数据的命令
select dbo.Accounts.balance
from dbo.ATMCards
INNER JOIN dbo.Accounts ON dbo.ATMCards.accountID = dbo.Accounts.accountID
where (dbo.ATMCards.cardNumber = @cardNumber)
似乎运行得很好添加了消息框来检查它感谢任何帮助欣赏它!
答案 0 :(得分:5)
您将信用卡号传递给参数@balanceDB
- 这是第一个错误。其次,您不使用ExecuteReader
执行更新 - 请改用ExecuteNonQuery
。
修改强>
我会为你做一些清理工作:
try
{
try
{
sqlCommandWithdraw.Connection.Open();
sqlCommandWithdraw.Parameters["@cardNumber"].Value = Class1.cardNumber;
// Make sure to dispose of the reader, which also closes the reader, which
// is important, because you can't perform any other selects on a connection
// with an open reader!
using (SqlDataReader reader = sqlCommandWithdraw.ExecuteReader())
{
// You will only get one line - also, your code also only evaluates
// one result, so we can do the following:
if (reader.Read())
{
balanceDB = decimal.Parse(readdata["balance"].ToString());
}
}
}
finally
{
sqlCommandWithdraw.Connection.Close();
}
decimal withdrawAmm = Convert.ToDecimal(textWithdraw.Text);
balanceDB = balanceDB - withdrawAmm;
try
{
sqlCommandUpdate.Connection.Open();
sqlCommandUpdate.Parameters["@cardNumber"].Value = Class1.cardNumber;
sqlCommandUpdate.Parameters["@balanceDB"].Value = balanceDB;
sqlCommandUpdate.ExecuteNonQuery();
MessageBox.Show(balanceDB +" Successfully Withdrawn");
}
finally
{
sqlCommandUpdate.Connection.Close();
}
}
答案 1 :(得分:1)
您为@balanceDB
参数传递了错误的值。它应该是余额。但是你传递了卡号。
sqlCommandUpdate.Parameters["@balanceDB"].Value = Class1.cardNumber;
应改为
sqlCommandUpdate.Parameters["@balanceDB"].Value = balanceDB ;
答案 2 :(得分:1)
这条线看起来很可疑:
sqlCommandUpdate.Parameters["@balanceDB"].Value = Class1.cardNumber;
那应该是Class1.balance
吗?