【发布时间】: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,尽管在数据库中,通过在查询窗口中运行相同的查询可以获得多个记录。但是我不知道如何通过编码得到它。
请帮帮我。
问候
【问题讨论】:
-
你应该使用parameterized SQL。
标签: c# database sql-server-2008 sql-server-2005