System.IndexOutOfRangeException:索引超出了数组的范围

时间:2012-01-01 12:17:13

标签: c# database sql-server-2008 sql-server-2005

我正在开发一个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:索引超出了数组的范围,尽管在数据库中有多个记录可用,它们通过在查询窗口中运行相同的查询来获得。但我不知道如何通过编码来实现它。

请帮帮我。

此致

4 个答案:

答案 0 :(得分:4)

像这样改变时间。

while (dr.Read())
{
    totalamount += Convert.ToDecimal(dr.GetString(0));
}

不需要i那里

答案 1 :(得分:3)

那是因为你试图读过太多的IMO专栏。

           while (dr.Read())
            {
                totalamount += Convert.ToDecimal(dr.GetString(i));

                i++;

            }

谁说列数多于行数? 看起来你想要总结一个专栏。

通过选择所有行来浪费时间。如果您正在寻找SUM,请使用SUM(COLUMN1)代替

                SqlCommand command = new SqlCommand("Select SUM(Amount) as sAmount 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(0));
                    break; // Only read once, since it returns only 1 line.

                }
                return totalamount;

答案 2 :(得分:2)

我认为问题就在这一行

 totalamount += Convert.ToDecimal(dr.GetString(i));
  i++;

为什么递增i?您无需增加i

i代表column index。您应该从同一列读取,因此您不需要增加i

建议您使用column name代替index

来检索值

答案 3 :(得分:0)

如果只获得一个值,请使用SqlCommand.ExecuteScalar,它返回一个值。

SqlCommand command = new SqlCommand("Select SUM(Amount) as TotalAmount From [Transaction] where AccountID = " + accountid + " AND CurrDate ='" + date + "' AND TransactionType = '" + transactiontype + "';", connection);   

connection.Open();   
decimal totalAmount = (decimal)command.ExecuteScalar();   

要避免SQL注入攻击,请考虑使用参数化命令。您可以在MSDN Documentation for SqlCommand中找到有关Execute.Scalar和Parametrized命令示例的信息。