【发布时间】:2011-01-16 10:13:37
【问题描述】:
我有一个非常奇怪的问题,我正在使用System.Data.SqlClient。
通过存储过程从 SQL Server 获取数据。
当我在 Development 和 Staging 机器上测试应用程序时,它工作正常,但是当我在 生产服务器 上部署应用程序时,我 随机 得到一个 SqlDataReader IndexOutOfRangeException 不同的列名!。
每1000个请求中有2个请求出现错误(近似值)。
SQL Server 是集群的
源代码:
public static List<CountryInfo> GetAllCountries(){
List<CountryInfo> Items = new List<CountryInfo>();
try{
using (rdr = SqlHelper.ExecuteReader(Globals.ConnectionString, "unv_spGetAllCountries"))
{
while (rdr.Read())
{
CountryInfo item = new CountryInfo();
item.CountryId = Convert.ToInt32(rdr["CountryId"]);
item.CountryName = rdr["CountryName"].ToString();
item.FirstLevel = rdr["FirstLevel"].ToString();
item.SecondLevel = rdr["SecondLevel"].ToString();
Items.Add(item);
}
}
}
catch (Exception ex)
{
throw ex;
}
Items.TrimExcess();
return Items;
}
存储过程:
select * from unv_tblCountries order by CountryName;
已经测试过
- 检查存储过程列名。
- 检查阅读器列名称。
- 检查连接字符串。
有人遇到过这样的问题并解决了吗?
【问题讨论】:
-
您必须提供更多详细信息,最好提供相关来源。没有它,就没什么好说的了。
-
我建议使用一个 catch 块来记录这个异常和所有传入的参数。
-
我添加了更多信息。
-
select * ...不会为您提供帮助,如果unv_tblCountries发生变化(或在不同情况下有所不同),您将在远处而不是在查询本身中遇到错误——这使得调试更难。因此,生产代码中的select *通常被认为是不良形式(即席查询不同)。 -
@Richad 我在 select 语句中添加了列,但问题仍然没有解决:(
标签: .net asp.net sqldatareader