而映射记录对象的DataReader挂 [英] DataReader hangs while mapping a record to an object
问题描述
我在哪里,我们查询数据库有超过20万条记录的一个项目中间,将几个一套过滤器我们的查询返回约200记录(等待约一时三十分分钟后)。查询数据库后,我尝试从这个特殊的DataReader创建对象,但每15或16记录了asp.net进程挂起(它挂在调试器),所以我认为这是一个DataReader的问题。
这是推动我疯了。
这里的code我用
使用(IDataReader的读卡器= cmd.ExecuteReader())
{
名单<项目>项目=新的名单,其中,项目>();
而(reader.Read())projects.Add(GetMappedRecord(读者));
}
私人项目GetMappedRecord(DataRow的读者)
{
项目项目=新项目();
project.PropertyA =读卡器[FIELDA]作为串;
//等等,等等...
返回的项目;
}
一个DataReader的问题?
如果您的通缉的写一张code,它的行为方式,你认为是DataReader的表现,你能做到吗?有时候,这是很好的想:如果我是一个错误,我会在哪里藏身,或在那里我能不能在隐瞒什么?
机会是有更多的了。
实验1:
使用(IDataReader的读卡器= cmd.ExecuteReader())
{
而(reader.Read())
;
}
看看是否能挂起。见的在的挂起(什么一套过滤器)。
当你得到一个挂(和一组过滤器),去尝试在Management Studio中的相同的SQL(具有相同参数)。看看是否能组合走的是一条很长的时间(如果你使用SQL Server 2008,请在活动监视器,看看你的查询是在慢列表)。
由于实验1成功,但它是一个问题的索引,让我们尝试以下操作:
尝试{
使用(IDataReader的读卡器= cmd.ExecuteReader())
{
而(reader.Read()){
的for(int i = 0; I< reader.FieldCount;我++){
对象ν= reader.GetValue(ⅰ);
}
}
}
}赶上(例外前){
Console.WriteLine(ex.ToString()); //或的MessageBox.show或什么
}
让我们看看,挂(多长时间!)
I'm in the middle of a project where we are querying a database with more than 20 million records, applying several set of filters our query returns about 200 records (after waiting for about 1.30 minutes). After querying the database I try to create objects from this particular datareader but once every 15 or 16 records the asp.net process hangs (it hangs in the debugger) so I think it is a DataReader issue.
This is driving me crazy.
Here's the code I'm using
using (IDataReader reader = cmd.ExecuteReader())
{
List<Project> projects = new List<Project>();
while(reader.Read()) projects.Add(GetMappedRecord(reader));
}
private Project GetMappedRecord(DataRow reader)
{
Project project = new Project();
project.PropertyA = reader["FieldA"] as string;
// and so on and so forth...
return project;
}
"A DataReader issue"?
If you wanted to write a piece of code that behaved the way you believe that DataReader is behaving, could you do that? Sometimes it's good to think, "If I were a bug, where would I be hiding", or "where could I not be hiding?"
Chances are there's more to it.
Experiment 1:
using (IDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
;
}
See if that hangs. See when it hangs (what set of filters).
When you get a hang (and a set of filters), go try out the same SQL (with the same parameters) in Management Studio. See if that combination is taking a long time (if you're using SQL Server 2008, look in Activity Monitor to see if your query is in the "slow" list).
Since experiment 1 succeeds, but it's an issue with the indexer, let's try the following:
try {
using (IDataReader reader = cmd.ExecuteReader())
{
while(reader.Read()) {
for (int i=0; i<reader.FieldCount; i++) {
object v = reader.GetValue(i);
}
}
}
} catch (Exception ex) {
Console.WriteLine(ex.ToString()); // Or MessageBox.Show or whatever
}
Let's see if that hangs (and for how long!)
这篇关于而映射记录对象的DataReader挂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!