【问题标题】:How do I connect to a SQL Server database using WebMatrix?如何使用 WebMatrix 连接到 SQL Server 数据库?
【发布时间】:2010-09-17 18:27:50
【问题描述】:

我添加了一个连接,它显示在“其他连接”中,但我似乎无法引用它。

var db = Database.Open("MyNewConnection");

抛出异常:Connection string 'MyNewConnection' was not found.

【问题讨论】:

    标签: sql-server webmatrix


    【解决方案1】:

    您是否检查过 web.config 文件中的连接字符串是否正确?

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
       <add 
         name="MyNewConnection" 
         connectionString= "server=myServer;database=myDatabase;uid=username;pwd=password"
         providerName="System.Data.SqlClient" /> 
    
      </connectionStrings>
    </configuration>
    

    【讨论】:

    • 奇怪的是,我没有看到它创建了 web.config 文件。事实上,我没有看到我的“其他连接”在任何地方持续存在。
    • 我相信Database.Open 在网站的 App_Data 文件夹中查找 (1) .sdf 文件或 (2) 在 web.config 中查找连接字符串。见:Working with Data - Connecting to a Database
    • 单击 WebMatrix 左下角的“数据库”选项卡后,左侧窗格中会列出“其他连接”。
    猜你喜欢
    • 2014-03-14
    • 2017-12-31
    • 2020-03-07
    • 2011-01-27
    • 2019-09-23
    • 1970-01-01
    • 2015-11-04
    • 2019-09-16
    相关资源
    最近更新 更多