我正在开发一种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