<分区>
我正在开发一个 ATM 软件作为家庭作业,我想知道今天处理的交易总额,为此我正在编写以下代码
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:索引超出数组范围,尽管在数据库中有多个记录可用,这些记录是通过在查询窗口中运行相同的查询获得的。但是我不知道如何通过编码得到它。
请帮帮我。
问候