Kevin

Kevin

  • NA
  • 1
  • 0

IOException: The Process cannot access file because it is in use by another process.

May 28 2008 5:45 AM

I am trying to populate a dataset with the results of an SQL Query...then...display those results in a browser, using XML and HTML.

public static void naming()

{

string query = "SELECT * FROM tbl_File_Health_Check " + //@Sql statement stored in variable query

"WHERE (File_Name LIKE '%~%' " +

"or file_name Like '%#%'" +

"or file_name Like '%&%'" +

"or file_name Like '%*%'" +

"or file_name Like '%{%'" +

"or file_name Like '%}%'" +

"or file_name Like '%\\%'" +

"or file_name Like '%:%'" +

"or file_name Like '%<%'" +

"or file_name Like '%>%'" +

"or file_name Like '%?%'" +

"or file_name Like '%/%'" +

"or file_name Like '%+%'" +

"or file_name Like '%|%'" +

"or file_name Like '%[%]%')"+

"ORDER BY File_Name";

using (SqlConnection conn = new SqlConnection("Server=ServerName;Database=File_Health_Check;Trusted_Connection=True;")) //constructs SqlConnection object and stores Database Connection string

{

SqlCommand cmd = new SqlCommand(query, conn);

conn.Open();

/*SqlDataReader reader = cmd.ExecuteReader();

 

while (reader.Read())

{

Console.SetWindowSize(100, 59);

Console.SetWindowPosition(0, 0);

Console.WriteLine(String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}",

reader[0], reader[1], reader[2], reader[3], reader[4], reader[5], reader[6]));

}

Console.WriteLine("");*/

try

{

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

adapter.Fill(ds);

ds.DataSetName = "Queries";

ds.Tables[0].TableName = "Naming Conventions";

DirectoryInfo tempDir = new DirectoryInfo("C:\\");

tempDir.CreateSubdirectory("tempWeb");

 

XmlDataDocument Xmldoc = new XmlDataDocument(ds);

XslCompiledTransform Xmltransform = new XslCompiledTransform();

FileInfo tempFile = new FileInfo(@"C:\tempWeb\Queries.xsl");

if (tempFile.Exists)

{

tempFile.Delete();

}

tempFile.Create();

FileStream FileStore = tempFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

FileStore.Close();

StreamWriter writer = new StreamWriter(@"C:\tempWeb\Queries.html");

writer.Close();

Xmltransform.Load(@"C:\tempWeb\Queries.xsl");

Xmltransform.Transform(Xmldoc, null, writer);

//reader.Close();

conn.Close();

}

catch (DirectoryNotFoundException e)

{

Console.WriteLine(e.Message);

}

catch (IOException e)

{

Console.WriteLine(e.Message);

}

catch (XsltException e)

{

Console.WriteLine(e.Message);

}

}

 

 

Console.WriteLine("Database Read...");

}

The break occurs when trying to load the.Xsl file with this statement.

Xmltransform.Load(@"C:\tempWeb\Queries.xsl");

 

I've tried closing "tempFile" after creation...however there is no method definition for Close(); with FileInfo.

To achieve some form of File closure...I stored "tempFile" within Filestream.  this however did not work.

any help would be great.


Answers (1)