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

时间:2023-01-14 20:06:54

I have a Visual Studio 2012 ASP.NET MVC application which queries a database. I've been told it's good practice to keep the connection string in the web.config file. The connection string called ConnString is located:

我有一个查询数据库的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>

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. How do I get the string?

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

3 个解决方案

#1


2  

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>

#2


1  

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

#3


1  

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. In the web.config you should have the System.Data.SqlClient as the value of the providerName attribute. It is the .NET Framework Data Provider you are using.

它的SQL Server .NET Framework数据提供程序。在web.config中,您应该将System.Data.SqlClient作为providerName属性的值。它是您正在使用的.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>

#1


2  

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>

#2


1  

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

#3


1  

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. In the web.config you should have the System.Data.SqlClient as the value of the providerName attribute. It is the .NET Framework Data Provider you are using.

它的SQL Server .NET Framework数据提供程序。在web.config中,您应该将System.Data.SqlClient作为providerName属性的值。它是您正在使用的.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>