首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebConfigurationManager未被识别

WebConfigurationManager未被识别
EN

Stack Overflow用户
提问于 2014-12-09 16:21:24
回答 2查看 1.6K关注 0票数 0

我是一个在建立连接方面的C#/MVC 4新手。直到现在,它一直在我工作之前就已经到位了,所以我从来没有考虑过它。但是,现在我需要建立到数据库的安全连接,并且没有任何工作示例可用作指南。

目前,我正试图使SqlCredential文档中的示例正常工作,但没有效果。他们似乎在这个实现中遗漏了很多东西。

是否有一个更完整的指南,我应该知道呢?

这是我的密码:

代码语言:javascript
复制
// 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”的定义,也不包含接受第一个参数的扩展.

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-09 16:24:24

它认为您在System.Configuration.Configuration (而不是System.Configuration 命名空间)中查找,尝试提供完整的名称空间:

代码语言:javascript
复制
Configuration config = System.WebConfiguration.WebConfigurationManager.OpenWebConfiguration(null);

另外,Null是一个错误,它应该是null

票数 1
EN

Stack Overflow用户

发布于 2014-12-10 13:36:17

我只想补充一下我的最终工作结果,以防其他人遇到同样的问题:

在看了这个关于设置.NET db连接的伟大系列文章的前3段视频后,我就可以连接起来了。我不知道为什么MS文档会使它变得过于复杂,但是kudvenkat让我明白了:

代码语言:javascript
复制
// 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获取配置字符串
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27383799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档