【问题标题】:how to connect to mdf file located in other directory (not in C:)如何连接到位于其他目录中的 mdf 文件(不在 C 中:)
【发布时间】:2016-08-24 13:52:38
【问题描述】:

我们如何连接到不在“C”中的其他目录中的 sql server mdf 文件。即文件位于“E”驱动器中。那么以 C# Windows 形式连接的连接字符串是什么。这是我的连接字符串。

<add name="ConnectionString" connectionString="Server=(local)\MSSQLSERVER;AttachDbFilename=E:\SaudiExchange.mdf;Database=SaudiExchange;Trusted_Connection=Yes;"
         providerName="System.Data.SqlClient"/>

【问题讨论】:

  • 这是在服务器端处理的。您应该连接到实例 (server\SQLEXPRESS) 并在连接字符串中指定数据库名称。
  • 我添加了我的连接字符串。看看吧。它不工作。
  • 创建一个扩展名为 .UDL 的文本文件,然后双击它。您现在将拥有一个表单,您可以在其中构建连接字符串并对其进行测试。如果连接字符串有效,则使用记事本打开 udl 文件,您将获得连接字符串
  • @GuidoG 它给了我异常(测试连接失败,因为初始化提供程序时出错”无法打开物理文件“E:\SaudiExchange.mdf”。操作系统错误 5:“5(访问被拒绝)")
  • 听起来像是一个安全问题。打开UDL文件的用户是否对E有读/写权限; ?

标签: c# winforms connection-string


【解决方案1】:

我认为你的connection string 必须是这样的:

<add name="ConnectionString" connectionString="Server=(local)\\MSSQLSERVER;AttachDbFilename=E:\\SaudiExchange.mdf;Database=SaudiExchange;Trusted_Connection=Yes;"
     providerName="System.Data.SqlClient"/>

对于本地连接,你可以试试这个:

<add name="ConnectionString" connectionString="Server=.;Initial Catalog=SaudiExchange;Trusted_Connection=Yes;"
     providerName="System.Data.SqlClient"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2016-08-15
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多