【问题标题】:System.IndexOutOfRangeException: Index was outside the bounds of the array [duplicate]System.IndexOutOfRangeException:索引超出了数组的范围[重复]
【发布时间】:2012-01-31 08:35:32
【问题描述】:

我正在开发一个 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: Index was outside the bounds of the array,尽管在数据库中,通过在查询窗口中运行相同的查询可以获得多个记录。但是我不知道如何通过编码得到它。

请帮帮我。

问候

【问题讨论】:

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


【解决方案1】:

像这样改变while。

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

那里不需要i

【讨论】:

    【解决方案2】:

    那是因为您试图阅读太多列 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;
    

    【讨论】:

      【解决方案3】:

      我认为问题出在这一行

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

      为什么要增加i 呢?你不需要增加i

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

      此外,建议使用column name 而不是index 检索值

      【讨论】:

        【解决方案4】:

        当您应该只获得一个值时,使用 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 和参数化命令示例的信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-11
          • 1970-01-01
          • 2011-04-11
          • 2019-12-11
          相关资源
          最近更新 更多