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

发布于 2024-12-10 04:28:39 字数 503 浏览 2 评论 0原文

我遇到了一个问题,同时学到了一些东西......

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

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

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

我尝试查看注册表,但没有运气...我尝试在整个硬盘驱动器中搜索该文件...没有运气。

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

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

有没有办法摆脱旧数据库文件“不存在”的痕迹

I got an issue and learned something at the same time....

I created a DBML from an existing server database.

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

Then I decided to delete it (MANUALLY, which is a bad thing evidentally) because when I try to recreate the database with the same name (eventhough the files are deleted), I get the error Database already exist. Choose a Different Name using CreateDatabase()

I tried looking through the registry, no luck... I tried searching the whole hard drive for the file.. no luck.

After googling, I found that you delete a database that was created with CreateDatabase() with DeleteDatabase().... Then you can recreate the database again.

Well problem is, now I still can't recreate the old database because the system thinks the name already exists.

Is there a way to get rid of the reminents of the old databse file the "does not exist"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俯瞰星空 2024-12-17 04:28:39

您需要通过服务器资源管理器master数据库> 在 Visual Studio 中(添加新连接 + 选择 master 数据库),然后添加一个新查询,输入 Drop Database xxxx 并执行它。您还可以使用Sql Server Management Studio。

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.

你是暖光i 2024-12-17 04:28:39

解决方案(通过 此处) 是使用 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();
        }
    }
}

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();
        }
    }
}
听风吹 2024-12-17 04:28:39

这是对 localDB 混乱的快速修复。只需连接到 SQL 管理中的 (localdb)\MSSQLLocalDB。工作室/视觉工作室。

SQL Server连接

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

在此处输入图像描述

Here's a quick fix for a localDB mess. Just connect to (localdb)\MSSQLLocalDB in SQL Mgmt. Studio/Visual Studio.

SQL Server Connection

Then delete the offender, checking "Close existing connections"

enter image description here

娇女薄笑 2024-12-17 04:28:39

我实际上也遇到了同样的问题。之前我删了
(剪切)mysqlserver2012中的数据库并将其复制到我的应用程序文件夹中。在制作应用程序后,我遇到了此错误,并通过删除连接字符串的 Initial Catalog 部分解决了该错误。

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

Data Source=;AttachDbFileName=database path\databaseName.mdf;Integrated Security=True" + ";User Instance=True" + ";Context Connection =错误;

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文