数据库已存在。使用CreateDatabase()选择不同的名称

时间:2011-10-15 02:43:23

标签: c# linq mdf

我遇到了一个问题并且同时学到了什么......

我从现有的服务器数据库创建了一个DBML。

从DBML我想创建本地数据库(.mdf文件)。我创建了数据库using DataContext.CreateDatabase("C:\xxxx.mdf")

然后我决定删除它(手动,这显然是一件坏事)因为当我尝试重新创建具有相同名称的数据库时(尽管文件被删除),我得到错误数据库已经存在。使用CreateDatabase()

选择不同的名称

我尝试通过注册表查看,没有运气......我尝试在整个硬盘上搜索文件..没有运气。

在谷歌搜索后,我发现您删除了使用CreateDatabase()创建的数据库DeleteDatabase() ....然后您可以重新创建数据库。

问题是,现在我仍然无法重新创建旧数据库,因为系统认为名称已经存在。

有没有办法摆脱旧数据库文件的重新组合“不存在”

4 个答案:

答案 0 :(得分:7)

您需要在Visual Studio中通过server explorer打开master数据库(添加新连接+选择master数据库),然后添加New query,输入Drop Database xxxx并执行它。您也可以使用Sql Server Management Studio.

答案 1 :(得分:3)

解决方案(通过here)是使用SSEUtil分离现有数据库:

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();
        }
    }
}

答案 2 :(得分:1)

这是localDB混乱的快速修复。只需连接到SQL Mgmt中的(localdb)\ MSSQLLocalDB。 Studio / Visual Studio。

SQL Server Connection

然后删除违规者,选中“关闭现有连接”

enter image description here

答案 3 :(得分:0)

我实际上也有同样的问题。以前,我删了 (剪切)mysqlserver2012中的数据库并将其复制到我的应用程序文件夹中。在我创建应用程序后,我收到了此错误,并通过删除我的连接字符串的DEPENDENCIES=TRUE部分来解决它。

您的最终连接字符串应如下所示:

Initial Catalog

相关问题