Database already exist. Choose a Different Name using CreateDatabase()

白昼怎懂夜的黑 提交于 2019-11-29 02:32:34

You need to open master database via server explorer in Visual Studio (Add New Connection + Select master database) then add a New query, type Drop Database xxxx and execute it. You can also use Sql Server Management Studio.

A solution (via here) is to use SSEUtil to detach the existing 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();
        }
    }
}
Rex Joshua

I also actually had this same problem. Previously, I deleted (cut) the database in mysqlserver2012 and copied it to my application folder. After I made my app I got this error, and solved it by removing the Initial Catalog part of my Connection String.

Your final connection string should look something like this:

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!