具有相同名称的数据库不存在,或者指定的文件无法打开,或它位于UNC共享 [英] A database with the same name exists, or specified file cannot be opened, or it is located on UNC share

查看:219
本文介绍了具有相同名称的数据库不存在,或者指定的文件无法打开,或它位于UNC共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到这个错误,当我跑我在新的PC项目。为了避免这种情况我必须复制每一次粘贴新的连接字符串..有什么办法避免......我有3个不同的数据库和它很讨厌O_O

 的SqlConnection CON =新的SqlConnection(数据源= \\\\ SQLEX $ P $干燥综合征; AttachDbFilename = D:\\\\ \\\\用户\\\\养老桌面\\\\ WAPent 3.0(1 )\\\\ WAPent 3.0 \\\\ WAPent 3.0 \\\\ WAPent 2.0 \\\\ WAPent 2.0 \\\\ \\\\的App_Data LoginStuff.mdf;集成安全=真;用户实例=真);

Web配置code

 <&是connectionStrings GT;
  <添加名称=ApplicationServices的connectionString =数据源= \\ SQLEX $ P $干燥综合征;集成安全性= SSPI; AttachDBFilename = | DataDirectory目录| \\ ASPNETDB.MDF;用户实例=真
   的providerName =System.Data.SqlClient的/>
  <添加名称=的ConnectionString的connectionString =数据源= \\ SQLEX $ P $干燥综合征。AttachDbFilename = | DataDirectory目录| \\ LoginStuff.mdf;集成安全=真;用户实例=真
   的providerName =System.Data.SqlClient的/>
 < /&是connectionStrings GT;


解决方案

在您使用的web.config | DataDirectory目录|这对于路径DataDirectory目录取代。这是通过使用设置

  AppDomain.CurrentDomain.SetData(DataDirectory目录的新路径)

当你不设置| DataDirectory目录|它默认为当它是一个web项目App_Data文件夹。
所以看你的code再由DataDirectory目录psented可能不包含数据库文件$ P $的路径。

有关DataDirectory目录的详细信息看看这个(以上)的文章

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

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>

解决方案

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

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.

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

这篇关于具有相同名称的数据库不存在,或者指定的文件无法打开,或它位于UNC共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆