System.IndexOutOfRangeException:索引是该数组的边界之外 [英] System.IndexOutOfRangeException: Index was outside the bounds of the array

查看:9120
本文介绍了System.IndexOutOfRangeException:索引是该数组的边界之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的ATM软件即在其中,我想知道这是今天处理的交易总额一家庭工作,为此我书面方式下面的代码

I am developing an ATM Software as a home work in which i want to know the total amount of transaction which is processed today, for this purpose I am writting the following code

 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:索引的数组的范围之外,虽然在数据库中的多个记录可这越来越通过运行在查询窗口相同的查询。但我不知道如何通过编码得到它。

But i am getting the exception System.IndexOutOfRangeException: Index was outside the bounds of the array, although in database more than one records are available which are getting by running the same query in query window. But I don't know how to get it through coding.

请帮我。

问候

推荐答案

那是因为你想阅读IMO太多列。

Thats because you're trying to read too many columns IMO.

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

                i++;

            }



谁说有比行更多的列?
好像你正在试图总结单列。

Who said there are more columns than rows? It seems like you're trying to sum a single column.

您是通过选择所有行浪费时间。如果你正在寻找的总和,使用 SUM(COLUMN1)而不是

You're wasting time by selecting all rows. if you're looking for the SUM, use SUM(COLUMN1) instead

                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;

这篇关于System.IndexOutOfRangeException:索引是该数组的边界之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆