LINQ TO SQL 中已有打开的与此命令相关联的 DataReader,必须首先将它关闭。问题
public bool IfSubsist(int stadiumid)
{
int count = (from c in DataContext.ConserveLocality
where c.userid == stadiumid
select c).Count();
return count > 0 ? true : false;
}
这个方法按F5按按几次就报 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。 --------------------编程问答-------------------- 问题不在这段代码 --------------------编程问答-------------------- 那要怎么弄列 --------------------编程问答-------------------- 这里Count()这个方法有可能error因为null
--------------------编程问答-------------------- public bool IfSubsist(int stadiumid)
public bool IfSubsist(int stadiumid)
{
try
{
int count = (from c in DataContext.ConserveLocality
where c.userid == stadiumid
select c).Count();
return count > 0 ? true : false;
}
catch
{retrun false;}
}
{
try
{
int count = (from c in DataContext.ConserveLocality
where c.userid == stadiumid
select c).ToList().Count;
return count > 0 ? true : false;
}
catch
{retrun false;}
}
也可以啊 不过 try 一下 比较保险 --------------------编程问答-------------------- public ConserveLocality GetConserveLocalityByuserid(int? userid, int? type)
{
try
{
ConserveLocality temp = (from a in DataContext.ConserveLocality
where a.userid == userid && a.type == type
select a).SingleOrDefault();
return temp;
}
catch (Exception e)
{
throw e;
}
}
已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
怎么解决呢?
我貌似程序里有很多这错哦 --------------------编程问答-------------------- --------------------编程问答-------------------- 这样也是没用的。 --------------------编程问答-------------------- 原因很可能是你使用了同个全局datacontext,
可以在linq查询的后面加个ToList()/ToArray()试下,再做相应的处理 --------------------编程问答-------------------- 太聪明了,用了全局的,datacontext,ok,let's go!
补充:.NET技术 , LINQ