Search code examples
c#linq-to-sqlexception

DataContext.CreateDatabase() says file already exists - but it doesn't


This might be a Windows 7 issue, but calling

using (var context = new DataClassesDataContext())
{
    if (!context.DatabaseExists())
    {
        context.CreateDatabase();
    }
}

Results in the following error:

System.Data.SqlClient.SqlException was unhandled Message=Database 'C:\Temp\SmallBusinessManager.mdf' already exists. Choose a different database name. Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=16 LineNumber=1 Number=1801 Procedure=""
Server=\.\pipe\952FCA9D-B4B6-4C\tsql\query State=3 StackTrace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Linq.SqlClient.SqlProvider.ExecuteCommand(String command) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.CreateDatabase() at System.Data.Linq.DataContext.CreateDatabase()

However I've deleted the database files. What could be causing this? Are the files really there but invisible somehow? Something to do with junctions?


Solution

  • Grab Process Monitor and you'll be able to see the file io operations and why they fail. I find that this is often the best and quickest way to get to the root of the problem. It's an excellent utility.