asp.net-mvc Visual Studio 2012 ASP.NET MVC 连接字符串 Web.Config
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17529016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio 2012 ASP.NET MVC Connection Strings Web.Config
提问by Jonathan
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 ConnStringis 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?
在页面顶部,但它仍然失败。我尝试使用using System.WebConfiguration,但仍然无法获取字符串。我如何获得字符串?
采纳答案by Ryan Weir
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>
回答by BizApps
You've missed to add providerName="System.Data.SqlClient"on your connection string.
您错过了添加providerName="System.Data.SqlClient"连接字符串。
Change your connectiong string to:
将连接字符串更改为:
<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
问候
回答by Microtechie
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.
它的 .NET Framework Data Provider for SQL Server。在 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>

