C# 存在同名数据库,或无法打开指定文件,或位于 UNC 共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18641883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:50:01  来源:igfitidea点击:

A database with the same name exists, or specified file cannot be opened, or it is located on UNC share

c#htmlasp.net

提问by user2625486

Getting this error when I run my project in new PC. TO avoid this I have to copy paste the new Connection string every time .. Is there any way to avoid that ... I have 3 different database and its very annoying O_O

当我在新 PC 上运行我的项目时出现此错误。为了避免这种情况,我必须每次都复制粘贴新的连接字符串.. 有什么办法可以避免这种情况......我有 3 个不同的数据库,它非常烦人的 O_O

SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Users\Yoro\Desktop\WAPent 3.0 (1)\WAPent 3.0\WAPent 3.0\WAPent 2.0\WAPent 2.0\App_Data\LoginStuff.mdf;Integrated Security=True;User Instance=True");

Web Config code

网页配置代码

    <connectionStrings>
  <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
   providerName="System.Data.SqlClient" />
  <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LoginStuff.mdf;Integrated Security=True;User Instance=True"
   providerName="System.Data.SqlClient" />
 </connectionStrings>

回答by Marco

In the web.config you are using |DataDirectory| which is a substitution for the path to the datadirectory. This is set by using

在 web.config 中,您正在使用 |DataDirectory| 这是对数据目录路径的替代。这是通过使用设置的

AppDomain.CurrentDomain.SetData("DataDirectory", newpath)

When you do not set |DataDirectory| it defaults to the App_Data folder when it is a web project. So looking at your code the path represented by DataDirectory probably does not contain the database file.

当您未设置 |DataDirectory| 时 当它是一个 web 项目时,它默认为 App_Data 文件夹。因此,查看您的代码,DataDirectory 表示的路径可能不包含数据库文件。

For more info about datadirectory have a look at this (older) article.

有关数据目录的更多信息,请查看这篇(较旧的)文章

回答by Asif Iqbal

This issue generally occurs in VS 2012 which I too have faced it. In order to resolve it,you need to foolow these steps: 1)Right Click on the .mdf file ->Select Modify connections under server Explorer. 2)Popup window will come->Click Advanced Button ->Select (LocalDB)\v11.0 as DataSource value

这个问题通常发生在 VS 2012 中,我也遇到过。为了解决它,您需要执行以下步骤: 1) 右键单击​​ .mdf 文件 -> 在服务器资源管理器下选择修改连接。2)弹出窗口->点击高级按钮->选择(LocalDB)\v11.0作为数据源值

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Also make sure you make changes in webconfig file as well for DataSource=(LocalDB)\v11.0 You are done.Wella!!

还要确保您在 webconfig 文件中也对 DataSource=(LocalDB)\v11.0 进行了更改,您已经完成了。Wella!!

回答by Asif Iqbal

Open server explorer, right-click on your database, select properties on bottom right corner. A property window will appear; copy the connection string there and use it in your application. For example, in my application:

打开服务器资源管理器,右键单击您的数据库,选择右下角的属性。将出现一个属性窗口;在那里复制连接字符串并在您的应用程序中使用它。例如,在我的应用程序中:

SqlConnection sconnection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Taha\Documents\Visual Studio 2013\Projects\Finder\Finder\App_Data\Userdb.mdf;
Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");

sconnection.Open();

回答by DCH

Exit Visual Studio.

退出 Visual Studio。

Run Visual Studio again.

再次运行 Visual Studio。

Do not open your project from last program list, open it using the open project -> browseprocedure.

不要从最后一个程序列表打开您的项目,使用打开项目 -> 浏览程序打开它。

Go to server explorer: if your .mdf file icon has a little red X, then create a new table, no need to enter anything in the table. Just update it.

转到服务器资源管理器:如果您的.mdf 文件图标有一个小红X,则创建一个新表,无需在表中输入任何内容。只需更新它。

First you will see a prompt: choose NO.

首先你会看到一个提示:选择NO

In the second prompt, choose YES: the little red X should go away.

在第二个提示中,选择YES:小红 X 应该消失。

Delete the table you just created and update.

删除您刚刚创建并更新的表。

This solved the problem for me.

这为我解决了这个问题。

回答by Dimitrie Vatra

First, your Database must be in other location than bin folder from your project; Then, your connection must be declared like this:

首先,您的数据库必须位于项目中 bin 文件夹以外的其他位置;然后,您的连接必须像这样声明:

SqlConnection con = new SqlConnection(TABLE1TableAdapter.Connection.ConnectionString);

If you have a DataGridView you can click on Choose Data Scource, and add your table as source from your database.(In my example, the table is named TABLE1.) Then, visual studio automatly create a DataSet, DataBinding, and a TableAdapter. The TableAdapter (TABLE1TableAdapter) contains as property the ConnectionString you need for database. P.S. I had this problem, and after a lot of work, i find this solution. I hope it helps you too.

如果您有 DataGridView,您可以单击“选择数据源”,然后将您的表添加为数据库中的源。(在我的示例中,该表名为 TABLE1。)然后,visual studio 自动创建一个 DataSet、DataBinding 和一个 TableAdapter。TableAdapter (TABLE1TableAdapter) 包含数据库所需的 ConnectionString 作为属性。PS我遇到了这个问题,经过大量工作,我找到了这个解决方案。我希望它也能帮助你。

回答by Bahaa Salaheldin

You should check your connection string. when you try to attach sql file the connection string should be like that: DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|PharmacyDB.mdf;Database=PharmacyDB;Integrated Security=True

您应该检查您的连接字符串。当您尝试附加 sql 文件时,连接字符串应如下所示: DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|PharmacyDB.mdf;Database=PharmacyDB;Integrated Security=True

回答by Ali Yousefi

I get this error when I use two connection string in my same DBContext,for example I try to Connect to sql server in a connection string and connect to sql express file in another connection string:

当我在同一个 DBContext 中使用两个连接字符串时出现此错误,例如我尝试在连接字符串中连接到 sql server 并在另一个连接字符串中连接到 sql express 文件:

    public MyDBContext(bool autoDetectChangesEnabled)
    : base("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;User Instance=false;Integrated Security=True;MultipleActiveResultSets=True")
    {
        Initialize(autoDetectChangesEnabled);
    }

    public MyDBContext()
     : base("data source=(LOCAL)\SQLEXPRESS;initial catalog=MyDB;persist security info=True;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework")
    {
        Initialize(true);
    }