我正在開發一種ATM軟件作爲家庭作業,其中我想知道今天處理的事務總量,爲此,我正在寫下面的代碼System.IndexOutOfRangeException:索引超出數組範圍
public decimal getDayTransaction(int accountid, string date, string transactiontype)
{
decimal totalamount = 0;
int i = 0;
string connectionString =
"Persist Security Info=False;User ID=sa; Password=123;Initial Catalog=ATMSoftware;Server=Bilal-PC";
try
{
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(
"Select Amount From [Transaction] where AccountID = "
+ accountid + " AND CurrDate ='" + date
+ "' AND TransactionType = '"
+ transactiontype + "';", connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
totalamount += Convert.ToDecimal(dr.GetString(i));
i++;
}
return totalamount;
}
}
catch (Exception e)
{
return -1;
}
}
但我得到異常System.IndexOutOfRangeException:索引是該數組的範圍之外,但在數據庫中的多個記錄,它們有越來越運行在查詢窗口相同的查詢。但我不知道如何通過編碼來獲得它。
請幫幫我。
Regards
您應該使用[參數化SQL](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter.aspx)。 – 2012-01-01 12:21:47