我有以下方法从表中提取信息;
public Customer GetCustomerDetails(int customerID)
{
Customer currentCustomer = null;
try
{
using (cxn = new SqlConnection(this.ConnectionString))
{
using (cmd = new SqlCommand("spGetCustomerInformation", cxn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerID", SqlDbType.Int).Value = customerID;
cxn.Open();
SqlDataReader dataReader = cmd.ExecuteReader();
currentCustomer = new Customer();
currentCustomer.CustomerID = customerID;
while (dataReader.Read())
{
currentCustomer.FirstName = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.firstName)).ToString();
currentCustomer.Surname = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.surname)).ToString();
currentCustomer.Email = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.email)).ToString();
currentCustomer.Phone = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.phone)).ToString();
currentCustomer.AddressLine1 = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.addressLine1)).ToString();
currentCustomer.AddressLine2 = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.addressLine2)).ToString();
currentCustomer.City = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.city)).ToString();
currentCustomer.County = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.county)).ToString();
}
dataReader.Close();
cxn.Close();
}
}
}
catch (SqlException)
{
throw;
}
return currentCustomer;
}
但是我一直收到错误“System.Data.dll中发生了'System.IndexOutOfRangeException'类型的未处理异常
附加信息:索引超出了数组的范围。“在while循环的最后一行。
我的SQL查询如下所示:
ALTER PROC [dbo].[spGetCustomerInformation]
@CustomerID INTEGER
AS
BEGIN
SET NOCOUNT ON
SELECT
a.Firstname,
a.Surname,
a.Email,
a.Phone,
a.AddressLine1,
a.AddressLine2,
a.City,
County
FROM
tblCustomers a
WHERE
a.CustomerID = @CustomerID
END
答案 0 :(得分:2)
CustomerTableColumns中有什么,您能告诉我们代码吗?
首先猜测,我会说你的枚举CustomerTableColumns的第一个值是1到8,而datareader预计会是0到7