2012-03-19 88 views
2
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"); 
} 

我工作的代碼自動取款機我有點東西在收回它看起來不錯,但似乎並沒有改變平衡,以反映在數據庫中的取款C#更新數據庫錯誤

我的命令是這樣的(更新)

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) 

似乎ŧ o運行得很好添加消息框檢查它感謝任何幫助感謝它!

+0

而你的問題是什麼?除非我錯過它... – 2012-03-19 15:11:08

+3

這看起來對我來說很可疑:sqlCommandUpdate.Parameters [「@ balanceDB」]。Value = Class1.cardNumber; – Liath 2012-03-19 15:12:31

+0

該查詢是否可更新?爲什麼你說沒有工作(在數據庫中沒有看到它明顯的旁邊..) – gbianchi 2012-03-19 15:13:50

回答

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(); 
    } 

}

+0

對此稍有不滿(雖然upvote是因爲我錯過了這個),但我相信你可以使用ExecuteReader進行更新,但不同的是它也會讀取選定的行。在這種情況下,你是正確的ExecuteNonQuery是正確的選擇,因爲沒有結果返回。 – Liath 2012-03-19 15:17:49

+1

出於興趣爲什麼嘗試/最後而不是使用如果你沒有捕捉到任何異常? – Liath 2012-03-19 15:22:59

+1

因爲OP顯然已經有了已經分配給連接的SQL命令的實例。如果在他的代碼中,連接和命令實例也被聲明瞭,那麼我會像使用臨時閱讀器一樣使用''使用'。然而,OP會打開連接,關閉它,然後關閉,但是他的代碼不會在異常時執行,所以我添加了try/finally,以便它更清晰。 – 2012-03-19 15:26:44

1

您爲@balanceDB參數傳遞了錯誤的值。它應該是平衡量。但是你傳遞的是卡號。

sqlCommandUpdate.Parameters["@balanceDB"].Value = Class1.cardNumber; 

應改爲

sqlCommandUpdate.Parameters["@balanceDB"].Value = balanceDB ; 
1

這行看起來可疑對我說:

sqlCommandUpdate.Parameters["@balanceDB"].Value = Class1.cardNumber; 

如果是這樣Class1.balance