加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – Visual Studio 2012 ASP.NET MVC连接字符串Web.C

发布时间:2020-12-16 03:55:36 所属栏目:asp.Net 来源:网络整理
导读:我有一个查询数据库的Visual Studio 2012 ASP.NET MVC应用程序.我被告知最好将连接字符串保存在web.config文件中.名为ConnString的连接字符串位于: connectionStrings add name="ConnString" connectionString="Data Source=IP_OF_SERVER,PORT; Initial Cat
我有一个查询数据库的Visual Studio 2012 ASP.NET MVC应用程序.我被告知最好将连接字符串保存在web.config文件中.名为ConnString的连接字符串位于:

<connectionStrings>
      <add name="ConnString" connectionString="Data Source=IP_OF_SERVER,PORT; Initial Catalog=DATABASE_NAME; UID=USERNAME; pwd=PASSWORD; Integrated Security=True;"/>
  </connectionStrings>

在C#中我想获取连接字符串,我使用:

String connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

应用程序在此行上死亡并抛出以下异常:

Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object.

我包括:

using System.Configuration;

在页面顶部,但它仍然失败.我尝试使用System.WebConfiguration,但我仍然无法获取字符串.我如何获得字符串?

解决方法

将您的web.config文件更改为包含providerName =“System.Data.SqlClient”作为连接字符串的属性,如下所示:

<connectionStrings>
      <add name="ConnString" connectionString="Data Source=IP_OF_SERVER,PORT; Initial Catalog=DATABASE_NAME; UID=USERNAME; pwd=PASSWORD; Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读