gpt4 book ai didi

c# - 系统.IndexOutOfRangeException : Index was outside the bounds of the array

转载 作者:太空狗 更新时间:2023-10-29 22:33:48 26 4
gpt4 key购买 nike

<分区>

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

请帮帮我。

问候

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com