I have one problem in my application. I need to create two CSV files from the two SQL query results. I created that one. I’m using StreamWriter to write the data into the CSV files.
My problem is first query result also written into the second file. I used StreamWriter.Close() and am still seeing the same behavior in the output.
using (StreamWriter sw = new StreamWriter(strFilePath, false))
{
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
}
}
}
The best way to ensure that a
StreamWriter(or in fact, anyIDisposableresource) is closed is to use theusingstatement where it is possible. Here is how:Once the scope of the
usingstatement ends, normally or through an exception, thewriteris closed automatically.