需要在下面的代码中包含条件。
private void button1_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection(@"connectionstring");
SqlCommand cmd = new SqlCommand("Command String", con);
SqlDataReader readdata;
try
{
con.Open();
decimal balance = decimal.Parse(textBox1.Text);
decimal Balance = 0.00M;
cmd.CommandText = "SELECT Balance FROM Accounts";
readdata = cmd.ExecuteReader();
while (readdata.Read())
{
Balance = decimal.Parse(readdata["Balance"].ToString());
}
con.Close();
con = new SqlConnection(@"connectionstring");
SqlCommand cmda = new SqlCommand("Command String", con);
con.Open();
if (Balance > balance)
{
Balance -= balance;
cmda.CommandText = "UPDATE Accounts SET Balance = '" + Balance + "' ";
cmda.ExecuteNonQuery();
con.Close();
con.Dispose();
label2.Content = "Withdrawal Success";
}
else
{
label2.Content = "Insufficient fund";
con.Close();
con.Dispose();
}
}
对于上述代码,用户可以从余额中提取任何金额,但我需要条件只提取100(100,200,300等)
答案 0 :(得分:0)
将if (Balance > balance)
更改为if (Balance > balance && balance % 100 == 0 )
。