简体   繁体   English

C#Windows应用程序附件文件名

[英]C# windows Application attachmentFilename

I have a working SQL connection here: 我在这里有一个有效的SQL连接:

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

but the problem is I need to provide the complete path. 但是问题是我需要提供完整的路径。 Unlike asp.net, all I have to do is to add |DataDirectory| 与asp.net不同,我要做的就是添加|DataDirectory| to it and include the database name .. 并包括数据库名称..

Then I tried to use the |DataDirectory| 然后,我尝试使用|DataDirectory| in this connection string and I get an error: 在此连接字符串中,我得到一个错误:

invalid value for key 'attachDbFilename' 密钥“ attachDbFilename”的值无效

I published my application and I can't locate the database now because I installed it in a different computer.. so the attachDbFilename is not true .. 我发布了我的应用程序,但现在找不到数据库,因为我将其安装在另一台计算机上。因此, attachDbFilename不是true ..

If the DataDirectory is not working is there any other code that I can use similar to data directory? 如果DataDirectory无法正常工作,还有其他类似于数据目录的代码可以使用吗?

AttachDbFilename is used with local database. AttachDbFilename与本地数据库一起使用。 If you have remote database then the connection string will be different . 如果您有远程数据库,则连接字符串将不同 Have a look at article - How to configure SQL Server 2005 to allow remote connections. 看一下文章- 如何配置SQL Server 2005以允许远程连接。

Access mdf file 访问mdf文件

As you see here 如你所见

System.Data.SqlClient resolves the substitution strings into full paths against the local computer file system. System.Data.SqlClient将替换字符串解析为本地计算机文件系统的完整路径。 Therefore, remote server, HTTP, and UNC path names are not supported. 因此,不支持远程服务器,HTTP和UNC路径名。 An exception is thrown when the connection is opened if the server is not located on the local computer. 如果服务器不在本地计算机上,则在打开连接时会引发异常。

If you want to connect to an already-running SQL Server Express instance on another computer over TCP, then you need to set-up TCP connections on that server first. 如果要通过TCP连接到另一台计算机上已经在运行的SQL Server Express实例,则需要首先在该服务器上设置TCP连接。 Note that you cannot use AttachDbFilename either because that option only applies to "user instances" of SQL Server Express. 请注意,您不能使用AttachDbFilename,因为该选项仅适用于SQL Server Express的“用户实例”。

Hope this will help you. 希望这会帮助你。

SqlConnection require you to specify the fully-qualified name of the db that is being attached. SqlConnection要求您指定要附加的数据库的标准名称。
No way around that. 没办法解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM