【问题标题】:Trying to make an SQL connection with local database .mdf but I succeed to write the filename directory尝试与本地数据库 .mdf 建立 SQL 连接,但我成功写入文件名目录
【发布时间】:2017-11-19 19:15:07
【问题描述】:

我有这条线用于连接我的连接字符串

SqlConnection conn = new SqlConnection("Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=""C: \Users\BDV\Documents\Visual Studio 2015\Projects\Interface_notes\Interface_notes\Database1.mdf"";Integrated Security=True;Connect Timeout=30");

但我收到此错误:

Class System.String
Represents Text as a series of Unicode characters
Syntax Error, ',' expected

我认为问题出在双引号上,但我不知道我能做什么。有人可以帮我弄这个吗?谢谢 无法识别的转义序列

【问题讨论】:

标签: c# sql sql-server visual-studio


【解决方案1】:

您的连接字符串中有两个错误。一个已经给你解释过了。这是 C#,您需要在字符串前面加上逐字字符,以避免在字符串包含反斜杠时出现解析错误。此外,您应该删除路径周围的双引号。这里不需要它们。

第二个错误是 (LocalDB) 和实例名称之间缺少反斜杠。

所以你写

 SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;
 AttachDbFilename=C:\Users\BDV\Documents\Visual Studio 2015\Project\Interface_notes\
 Interface_notes\Database1.mdf;
 Integrated Security=True;Connect Timeout=30");

【讨论】:

    【解决方案2】:

    您可以使用逐字字符串并从内部删除双引号:

    改成

    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;
         AttachDbFilename=C:\Users\BDV\Documents\Visual Studio 2015\Project\Interface_notes\
         Interface_notes\Database1.mdf;
         Integrated Security=True;Connect Timeout=30");
    

    【讨论】:

      【解决方案3】:

      应该是这样的

      SqlConnection conn = new SqlConnection(@"Data Source=
      (LocalDB)MSSQLLocalDB;AttachDbFilename=C:\Users\BDV\Documents\Visual Studio 
      2015\Projects\Interface_notes\Interface_notes\Database1.mdf;Integrated 
      Security=True;Connect Timeout=30");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 2021-12-07
        • 1970-01-01
        相关资源
        最近更新 更多