System.IndexOutOfRangeException:索引超出数组范围
我正在开发一个 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:索引超出范围数组的一部分,尽管在数据库中可以通过在查询窗口中运行相同的查询来获取多个记录。但我不知道如何通过编码来获得它。
请帮我。
问候
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;
}
}
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.
Please help me.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样改变 while 。
那里不需要
i
Change the while like this.
There is no need of an
i
there在我看来,那是因为您试图阅读太多专栏。
谁说列多于行?
看起来您正在尝试对单列求和。
选择所有行是在浪费时间。如果您要查找 SUM,请改用
SUM(COLUMN1)
Thats because you're trying to read too many columns IMO.
Who said there are more columns than rows?
It seems like you're trying to sum a single column.
You're wasting time by selecting all rows. if you're looking for the SUM, use
SUM(COLUMN1)
instead我认为问题出在这一行为
什么要增加
i
for?您不需要增加i
i
代表这里的列索引
。您应该从同一列读取,因此不需要增加i
。此外,建议使用
列名称
而不是索引
检索值I think problem is in this line
Why are incrementing
i
for? you don't need to incrementi
i
represents thecolumn index
here. You are suppose to read from same column so you don't need to incrementi
.Also it is a recommended practise to retrieve value using
column name
instead ofindex
当您只需要获取一个值时,请使用 SqlCommand.ExecuteScalar,它返回单个值。
为了避免 SQL 注入攻击,请考虑使用参数化命令。您可以在 SqlCommand 的 MSDN 文档。
When you should get only one value, use the SqlCommand.ExecuteScalar, which returns a single value.
To avoid SQL injection attacks, consider to use parameterized commands. You can find information about Execute.Scalar and Parametrized command example in the MSDN Documentation for SqlCommand.