我创建了一个方法(OutputFile
),该方法使用SqlDataReader
接收数据,然后使用StreamWriter
将其输出到本地文件。
为了测试该方法输出正确的数据,我创建了一个控制台应用程序,当从中调用该函数时,它需要大约 4分钟,这是预期的,因为所涉及的存储过程返回了很多行(〜200,000)。
public void OutputFile(string query, string path, params object[] parameters)
{
string connectionString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
// Open connection
con.Open();
Debug.WriteLine("Starting FileStream");
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
Debug.WriteLine("Execute Reader");
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
Debug.WriteLine("Writing Files");
while (reader.Read())
{
char[] buffer = new char[4096];
int charsRead = 0;
using (TextReader data = reader.GetTextReader(0))
{
charsRead = data.Read(buffer, 0, buffer.Length);
if (charsRead > 0)
{
writer.WriteLine(buffer, 0, charsRead);
}
}
}
}
}
}
}
}
}
当我将方法移动到ASP.NET MVC应用程序时,执行时间大大增加。我第一次运行应用程序时, 14分钟输出文件,第二次 30分钟。我已经在下面的调试窗口中包含了输出。
运行应用程序时,我确保将检查抛出的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).
答案 0 :(得分:0)
线程退出消息来自Web服务器上的其他线程,可能不一定是您的请求线程(因为Web服务器是多线程的)
尝试使用StringBuilder,而不是使用System.IO.File.WriteAllText而不是使用文件流将所有内容写入文件 如果你需要附加到现有文件System.IO.File.AppendAllText也可以工作
此外,对于SQL,尝试使用SqlDataAdapter并将整个结果复制到DataSet中并使用Table [0]循环遍历数据
希望这能解释