DataReader 索引超出数组范围
DataReader index outside bounds of Array
我使用以下方法从 table;
中提取信息
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.IndexOutOfRangeException' 类型的未处理异常发生在 System.Data.dll
附加信息:索引超出数组范围。”在 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
CustomerTableColumns 中有什么,您能告诉我们代码吗?
乍一看,我会说您的枚举 CustomerTableColumns 的第一个值是 1 到 8,而数据读取器预计是 0 到 7
我使用以下方法从 table;
中提取信息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.IndexOutOfRangeException' 类型的未处理异常发生在 System.Data.dll
附加信息:索引超出数组范围。”在 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
CustomerTableColumns 中有什么,您能告诉我们代码吗?
乍一看,我会说您的枚举 CustomerTableColumns 的第一个值是 1 到 8,而数据读取器预计是 0 到 7