简体   繁体   English

Visual Studio 2012 ASP.NET MVC连接字符串Web.Config

[英]Visual Studio 2012 ASP.NET MVC Connection Strings Web.Config

I have a Visual Studio 2012 ASP.NET MVC application which queries a database. 我有一个查询数据库的Visual Studio 2012 ASP.NET MVC应用程序。 I've been told it's good practice to keep the connection string in the web.config file. 我被告知最好将连接字符串保存在web.config文件中。 The connection string called ConnString is located: 名为ConnString的连接字符串位于:

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

In C# where I want to get the connection string, I use: 在C#中我想获取连接字符串,我使用:

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

The app dies on this line and throws the following exception: 应用程序在此行上死亡并抛出以下异常:

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

I have included: 我包括:

using System.Configuration;

at the top of the page, but it still fails. 在页面顶部,但它仍然失败。 I tried using using System.WebConfiguration , but I can still not get the string. 我尝试使用using System.WebConfiguration ,但我仍然无法获取字符串。 How do I get the string? 我如何获得字符串?

Change your web.config file to include providerName="System.Data.SqlClient" as an attribute on the connection string like this: 将您的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>

You've missed to add providerName="System.Data.SqlClient" on your connection string. 您错过了在连接字符串上添加providerName="System.Data.SqlClient"

Change your connectiong string to: 将connectiong字符串更改为:

  <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>

Regards 问候

I have nothing new to answer this question but I would like to give some explanation, 我没有什么新的回答这个问题,但我想作一些解释,

 System.Data.SqlClient 

Its .NET Framework Data Provider for SQL Server. 它的SQL Server .NET Framework数据提供程序。 In the web.config you should have the System.Data.SqlClient as the value of the providerName attribute. 在web.config中,您应该将System.Data.SqlClient作为providerName属性的值。 It is the .NET Framework Data Provider you are using. 它是您正在使用的.NET Framework数据提供程序。

if you have to connect your application with MYSql then you may use 如果您必须使用MYSql连接您的应用程序,那么您可以使用

  MySql .Net Connector

Its required but in your case its missing, thats why you are getting an error message. 它是必需的,但在你的情况下它缺少,这就是你收到错误信息的原因。

You can read more about (here)[http://msdn.microsoft.com/en-US/library/htw9h4z3(v=VS.80).aspx] Hope it will give you a better understanding what error you made and you yo fix it. 你可以阅读更多关于(这里)[http://msdn.microsoft.com/en-US/library/htw9h4z3(v = VS.80).aspx]希望它能让你更好地理解你犯的错误和你你解决它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM