The database already exists. Choose a different name using CreateDatabase ()

I had a problem and learned something at the same time.

I created DBML from an existing server database.

From DBML, I wanted to create a local database (.mdf file). I created a database using DataContext.CreateDatabase("C:\xxxx.mdf").

Then I decided to delete it (MANUALLY, which is hard to see), because when I try to recreate the database with the same name (while the files are deleted), I get an error The database already exists. Choose a different name using CreateDatabase ()

I tried to look at the registry, no luck ... I tried to search the entire hard drive for the file .. no luck.

After googling, I found that you deleted the database created with CreateDatabase(), using DeleteDatabase().... Then you can create the database again.

The problem is that now I still can’t recreate the old database because the system thinks that the name already exists.

Is there a way to get rid of the memories of the old database file "does not exist"

+5
source share
3 answers

You need to open master using server explorerin Visual Studio (add new connection + select masterdatabase), then add New query, enter Drop Database xxxxand execute it. You can also useSql Server Management Studio.

+7
source

( ) SSEUtil db:

try
{
    // open a connection to the database for test
}
catch (SystemException ex) // Change exception type based on your underlying data provider
{
    if (ex.Message.ToLower().Contains("already exists. choose a different database name"))
    {
        var match = Regex.Match(ex.Message, "database '(.*)' already exists.", 
           RegexOptions.IgnoreCase);

        if (match.Success)
        {
            String dbFileName = match.Groups[1].Value;
            Process p = new Process();
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.FileName = String.Format("{0}/Tools/SSEUtil.exe", 
              Environment.CurrentDirectory);
            p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
            p.StartInfo.Arguments = String.Format("-d \"{0}\"", dbFileName);

            p.Start();
        }
    }
}
+3

. () mysqlserver2012 . , , , Initial Catalog .

:

Data Source=<your server name>;AttachDbFileName=database path\databaseName.mdf;Integrated Security=True" + ";User Instance=True" + ";Context Connection=False;

-1

All Articles