间歇列不属于表异常

时间:2011-08-12 16:43:46

标签: sql stored-procedures

我有一个每分钟被调用数百次的存储过程。每隔一段时间我都会遇到一个异常,即查询中不存在列。这是一个例外:

System.Web.HttpUnhandledException:抛出了类型'System.Web.HttpUnhandledException'的异常。 ---> System.ArgumentException:列'SubjectID'不属于表。

[ArgumentException:列'SubjectID'不属于表。]    System.Data.DataRow.GetDataColumn(String columnName)+1775157    System.Data.DataRow.get_Item(String columnName)+13    System.Data.DataTableReader.get_Item(String name)+66

这是调用存储过程的C#:

DataTableReader dtr;
        dtr = Util.getDepartmentsByTerm(term);
        ddlSubject.Items.Add(new ListItem("Select A Subject...", ""));
        while (dtr.Read())
        {
            //The following line throws the exception!
            var value = (string)dtr["SubjectID"];
            var text = (string)dtr["title"];
            var count = (int) dtr["Count"];
            //if (!text.Contains("(0)"))
            if(count > 0)
                ddlSubject.Items.Add(new ListItem(text, value));
        }

非常简单......这是存储过程:

ALTER PROCEDURE [dbo].[selectDepartmentsByTerm] 

( 

@term nvarchar(50),
@version int

) 

AS     
SELECT SubjectID, Subject as Title,(select count(distinct courseno)
  from RegistrationBlock WITH (NOLOCK)
 where RegistrationBlock.Subject = Subjects.SubjectID
   and RegistrationBlock.version = @version
   and (@term = '' OR RegistrationBlock.term = @term)) as "Count"
  FROM Subjects WITH (NOLOCK)
 where Version = @version
 order by subject
GO

1 个答案:

答案 0 :(得分:1)

Tom H.对我的问题的评论解决了这个问题:

  

我看过一篇帖子说这个问题可能是由连接池中的损坏引起的。确保正确关闭所有连接。您可以在此处阅读更多诊断/解决问题的方法:http://www.ksvali.com/2010/08/solution-to-random-error-column-does-not-belong-to-table/