2015-09-07 84 views
1

我这里有一个工作的SQL连接:C#Windows应用程序attachmentFilename

SqlConnection conn = 
    new SqlConnection(@"server=localhost\SQLEXPRESS; 
         AttachDbFilename=C:\Seach ENGINE (June 22, 2015)\SE\SE\MainDatabase.mdf; 
         Integrated Security=True;User Instance=True"); 

,但问题是我需要提供完整路径。不像asp.net,所有我需要做的就是添加|DataDirectory|它和包括数据库名..

然后我尝试这个连接字符串中使用|DataDirectory|和我得到一个错误:

invalid value for key 'attachDbFilename'

我出版了我的应用程序,因为我在不同的计算机安装了它,我现在不能定位数据库..所以attachDbFilename是不正确的..

如果DataDirectory不工作是否有任何其他的代码,我可以使用类似于数据目录?

+0

两台机器都已连接? – Sunny

+0

是的,先生..多数民众赞成在 – kaizoshi

+0

为什么你不使用IP? – Sunny

回答

0

SqlConnection要求您指定要连接的db的 db的完全限定名称。
没办法。

1

AttachDbFilename与本地数据库一起使用。如果您有远程数据库,则连接字符串将为be different。看看文章 - How to configure SQL Server 2005允许远程连接。

Access mdf file

正如你在这里看到的

System.Data.SqlClient resolves the substitution strings into full paths against the local computer file system. Therefore, remote server, HTTP, and UNC path names are not supported. An exception is thrown when the connection is opened if the server is not located on the local computer.

如果你想连接到一个已经运行的SQL Server Express实例的另一台计算机通过TCP上,那么你需要建立TCP连接上该服务器第一。请注意,您不能使用AttachDbFilename,因为该选项仅适用于SQL Server Express的“用户实例”。

希望这会帮助你。