【问题标题】:WebConfigurationManager is unrecognizedWebConfigurationManager 无法识别
【发布时间】:2014-12-09 16:21:24
【问题描述】:

我是 C#/MVC 4 设置连接的菜鸟。直到现在,它总是在我开始工作之前就已经到位,所以我从来不用考虑它。但是,现在我需要建立与数据库的安全连接,并且没有任何工作示例可用作指导。

我目前正在尝试让SqlCredential documentation 中的示例工作,但无济于事。他们似乎在这个实现中遗漏了很多东西。

是否有我应该注意的更完整的指南?

这是我的代码:

// Web.config:
<connectionStrings>
    <add name="Con" connectionString="Initial Catalog=myDB;Server=\\Server_name" providerName="System.Data.SqlClient" />
</connectionStrings>

// SearchController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient; // for SqlConnection & SqlCommand
using System.Configuration;
using System.Web.Configuration;
using System.Security;

Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration(Null);
ConnectionStringSettings connString = config.ConnectionStrings.ConnectionString[“Con”];

using (SqlConnection conn = new SqlConnection(connString.ConnectionString))
{
    SecureString pwd = new SecureString();
    pwd.Equals("PASSW0rd");
    pwd.MakeReadOnly();
    SqlCredential cred = new SqlCredential("my_user_id", pwd);

    SqlCommand comm = new SqlCommand("SELECT * FROM Search", conn);
    conn.Credential = cred;
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
}

WebConfigurationManager 出现错误:

“System.Configuration.Configuration”不包含定义 'WebConfigurationManager'

Null 有错误:

当前上下文中不存在名称“Null”

ConnectionString["Con"] 出现错误:

'System.Configuration.ConnectionStringsSection' 不包含 'ConnectionString' 的定义并且没有扩展接受第一个 论据...

另外,有没有更好的地方放置我的密码和用户名?在示例中,它使用文本框来接受这些作为输入,但在我的情况下,它们是已知的并且不会改变。

【问题讨论】:

标签: c# asp.net .net asp.net-mvc-4


【解决方案1】:

它认为您正在寻找 System.Configuration.Configuration class 中的 WebConfigurationManager,而不是 System.Configuration 命名空间,请尝试提供完整的命名空间:

Configuration config = System.WebConfiguration.WebConfigurationManager.OpenWebConfiguration(null);

另外Null 是一个错字,应该是null

【讨论】:

  • 我尝试了 System.Web.Configuration,并且成功了,但 ConnectionString 仍然有错误。
  • 您是否包含了 System.Configuration 参考?
  • 是的(我的所有内容都包含我提供的代码)。
  • 我发现了问题所在。在我从 MS 站点复制的代码中,它显示 ConnectionString。它需要是 ConnectionStrings。显然他们没有测试他们的任何例子。什么鬼?
  • @Tra:是的,它发生了。而且比你想象的更频繁。很高兴你修好了。
【解决方案2】:

我只是想添加我的最终工作结果,以防其他人遇到同样的问题:

观看this great series on setting up .NET db connections 的前 3 个视频后,我能够建立连接。我不确定为什么 MS 文档让它过于复杂,但 kudvenkat 让我理顺了:

// web.config   
<add name="MyDataConnection" 
     connectionString="data source=server_name; database=myDB; user id=my.user.iDB password=PASSW0rd"        
     providerName="System.Data.SqlClient" 
/>

// SearchController.cs
string CS = ConfigurationManager.ConnectionStrings["MyDataConnection"].ConnectionString;
using (SqlConnection connection = new SqlConnection(CS))
    {
        SqlCommand command = new SqlCommand("SELECT * FROM Search", connection);
        connection.Open();
        SqlDataReader rdr = command.ExecuteReader();
    }

指出一些重要的学习点:

  • 连接字符串不必在那个复杂的 ConfigurationStringsSettings 对象中,它可以只是一个字符串。
  • 密码可以在web.config中的连接字符串中定义
  • 您不需要使用 System.Web.Configuration 从 Web.config 获取配置字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2013-04-19
    • 2019-01-28
    • 2017-08-10
    相关资源
    最近更新 更多