none
How to detach database? RRS feed

  • Question

  • Hi

    i have such code

     

     

    try
    			{
    				SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    				builder.InitialCatalog = "QQQQQ";
    				builder.DataSource = @".\SQLEXPRESS";
    				builder.IntegratedSecurity = true;
    				builder.AttachDBFilename = @"D:\temp.mdf";
    
    				using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
    				{
    					connection.Open();
    					// Now use the open connection.
    				}
    
    				System.IO.File.Delete(@"D:\temp.mdf");
    			}
    			catch (Exception ex)
    			{
    				MessageBox.Show(ex.Message);
    			}

     The line

    System.IO.File.Delete(@"D:\temp.mdf");

    throws an exception: "The process cannot access the file 'D:\\temp.mdf' because it is being used by another process."

    What i must do to unlock  temp.mdf ?

     

     

     

     

    • Moved by Jeff Derstadt - MSFT Sunday, May 23, 2010 5:36 AM SqlClient Issue (From:ADO.NET Entity Framework and LINQ to Entities)
    Saturday, May 22, 2010 11:16 AM

Answers