Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC应用程序与控制台应用程序中使用SqlDataReader、StreamWriter和FileStream时的奇怪行为_C#_Asp.net_Asp.net Mvc_Streamwriter_Sqldatareader - Fatal编程技术网

C# 在MVC应用程序与控制台应用程序中使用SqlDataReader、StreamWriter和FileStream时的奇怪行为

C# 在MVC应用程序与控制台应用程序中使用SqlDataReader、StreamWriter和FileStream时的奇怪行为,c#,asp.net,asp.net-mvc,streamwriter,sqldatareader,C#,Asp.net,Asp.net Mvc,Streamwriter,Sqldatareader,我创建了一个方法(OutputFile),该方法使用SqlDataReader接收数据,然后使用StreamWriter将其输出到本地文件 为了测试该方法是否输出了正确的数据,我创建了一个控制台应用程序,当从该应用程序调用函数时,大约需要4分钟,这是预期的,因为所涉及的存储过程返回了大量行(~200000) 当我将该方法移到ASP.NET MVC应用程序中时,执行时间大大增加。第一次运行应用程序时,我用了14分钟输出文件,第二次用了30分钟。我已经包含了下面调试窗口的输出 在运行应用程序时,我确

我创建了一个方法(
OutputFile
),该方法使用
SqlDataReader
接收数据,然后使用
StreamWriter
将其输出到本地文件

为了测试该方法是否输出了正确的数据,我创建了一个控制台应用程序,当从该应用程序调用函数时,大约需要4分钟,这是预期的,因为所涉及的存储过程返回了大量行(~200000)

当我将该方法移到ASP.NET MVC应用程序中时,执行时间大大增加。第一次运行应用程序时,我用了14分钟输出文件,第二次用了30分钟。我已经包含了下面调试窗口的输出

在运行应用程序时,我确保打开check抛出的CLR异常,但没有抛出任何异常

提前感谢您提供的任何帮助,可以解释为什么会发生此问题

关于ASP.NET MVC应用程序的附加说明(控制台应用程序不包括这些引用)

  • 使用实体框架和统一框架
  • 使用一瞥
调试14分钟内生成的文件的输出

Starting FileStream
Execute Reader
The thread 0x56c has exited with code 259 (0x103).
The thread 0x1194 has exited with code 259 (0x103).
The thread 0x568 has exited with code 0 (0x0).
The thread 0x13f8 has exited with code 259 (0x103).
The thread 0xb6c has exited with code 259 (0x103).
Writing Files
The thread 0xfa8 has exited with code 259 (0x103).
The thread 0x136c has exited with code 259 (0x103).
调试30分钟内生成的文件的输出

Starting FileStream
Execute Reader
The thread 0x1290 has exited with code 259 (0x103).
The thread 0x1030 has exited with code 259 (0x103).
The thread 0x11cc has exited with code 259 (0x103).
The thread 0x1310 has exited with code 259 (0x103).
The thread 0x1d4 has exited with code 259 (0x103).
The thread 0xbb8 has exited with code 259 (0x103).
The thread 0x179c has exited with code 259 (0x103).
The thread 0x1390 has exited with code 259 (0x103).
The thread 0x169c has exited with code 259 (0x103).
The thread 0x16a0 has exited with code 259 (0x103).
The thread 0x1424 has exited with code 259 (0x103).
The thread 0x16e8 has exited with code 259 (0x103).
The thread 0x14f0 has exited with code 259 (0x103).
The thread 0x1458 has exited with code 259 (0x103).
The thread 0x17f8 has exited with code 259 (0x103).
The thread 0x1094 has exited with code 259 (0x103).
The thread 0x1484 has exited with code 259 (0x103).
The thread 0x1540 has exited with code 259 (0x103).
The thread 0x1694 has exited with code 259 (0x103).
The thread 0x17c8 has exited with code 259 (0x103).
The thread 0x1660 has exited with code 259 (0x103).
The thread 0x1764 has exited with code 259 (0x103).
The thread 0x1548 has exited with code 259 (0x103).
The thread 0x172c has exited with code 259 (0x103).
The thread 0x17d8 has exited with code 259 (0x103).
The thread 0x1674 has exited with code 259 (0x103).
The thread 0x1380 has exited with code 259 (0x103).
Writing Files
The thread 0x16a8 has exited with code 259 (0x103).

线程退出消息来自web服务器上的其他线程,不一定是您的请求线程(因为web服务器是多线程的)

尝试使用StringBuilder,然后使用System.IO.file.WriteAllText将所有内容写入文件,而不是使用文件流 如果需要附加到现有的文件系统中,也可以使用.IO.file.AppendAllText

此外,对于SQL,请尝试使用SqlDataAdapter并将整个结果复制到数据集中,然后使用表[0]循环数据


希望这能解释SQL Server profiler是怎么说的?SQL代码在MVC中的执行时间与在console应用程序中的执行时间相同吗?验证代码是否执行相同的SQL并返回相同的结果可能是有用的。SQL Server Profiler可以显示,很难说出您发布的内容或是否存在问题。需要记住的一点是,SqlDataReader将在第一行到达时立即开始读取行,因此ExecuteReader()的“计时”没有意义。如果您想处理一些事情,您应该使用整个数据读取器,然后执行文件写入。通过这种方式,您可以看到在哪里集中精力。存储过程是否总是返回相同数量的行,并且您是否总是在相同的环境中运行?我注意到您的Debug.WriteLine方法没有很好地指示每次读取的数据量。因此,如果14分钟的跑量是20万排,那么我几乎预计30分钟的跑量将接近40万排。通过分析这一点,您可以了解是否存在效率低下的问题。@SergeyLitvinov当我运行控制台应用程序时,存储过程的执行会立即出现在SQL Server Profiler中。我十分钟前开始运行MVC应用程序,但SQL Server Profiler中仍然没有存储过程的执行。您是否能够就导致MVC应用程序中存储过程执行延迟的原因提供建议?@cmcquillan存储过程始终返回相同数量的行,并且MVC应用程序和控制台应用程序生成的文件完全匹配。
Starting FileStream
Execute Reader
The thread 0x1290 has exited with code 259 (0x103).
The thread 0x1030 has exited with code 259 (0x103).
The thread 0x11cc has exited with code 259 (0x103).
The thread 0x1310 has exited with code 259 (0x103).
The thread 0x1d4 has exited with code 259 (0x103).
The thread 0xbb8 has exited with code 259 (0x103).
The thread 0x179c has exited with code 259 (0x103).
The thread 0x1390 has exited with code 259 (0x103).
The thread 0x169c has exited with code 259 (0x103).
The thread 0x16a0 has exited with code 259 (0x103).
The thread 0x1424 has exited with code 259 (0x103).
The thread 0x16e8 has exited with code 259 (0x103).
The thread 0x14f0 has exited with code 259 (0x103).
The thread 0x1458 has exited with code 259 (0x103).
The thread 0x17f8 has exited with code 259 (0x103).
The thread 0x1094 has exited with code 259 (0x103).
The thread 0x1484 has exited with code 259 (0x103).
The thread 0x1540 has exited with code 259 (0x103).
The thread 0x1694 has exited with code 259 (0x103).
The thread 0x17c8 has exited with code 259 (0x103).
The thread 0x1660 has exited with code 259 (0x103).
The thread 0x1764 has exited with code 259 (0x103).
The thread 0x1548 has exited with code 259 (0x103).
The thread 0x172c has exited with code 259 (0x103).
The thread 0x17d8 has exited with code 259 (0x103).
The thread 0x1674 has exited with code 259 (0x103).
The thread 0x1380 has exited with code 259 (0x103).
Writing Files
The thread 0x16a8 has exited with code 259 (0x103).