C# MVC 4 连接字符串到 SQL Server 2012
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13000824/
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
MVC 4 Connectionstring to SQL Server 2012
提问by lhan
I've created a brand new MVC 4 application in C# using Visual Studio 2012. I'm trying to connect to a brand new SQL Server 2012 (Standard) instance but I can't seem to get my connection string set correctly.
我已经使用 Visual Studio 2012 在 C# 中创建了一个全新的 MVC 4 应用程序。我正在尝试连接到一个全新的 SQL Server 2012(标准)实例,但我似乎无法正确设置我的连接字符串。
My connection string from my Web.config:
我的 Web.config 中的连接字符串:
<connectionStrings>
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=MyServerName;
Initial Catalog=MyDatabaseName;
Integrated Security=False;
User ID=MyUserID;Password=MyPassword;
MultipleActiveResultSets=True" />
</connectionStrings>
Every time I go to ASP.NET Configuration from within Visual Studio, the page loads, but as soon as I click "Security" I get the following message:
每次我从 Visual Studio 中转到 ASP.NET 配置时,页面都会加载,但是一旦我单击“安全”,我就会收到以下消息:
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.
The following message may help in diagnosing the problem: Unable to connect to SQL Server database.
您选择的数据存储有问题。这可能是由于服务器名称或凭据无效,或者权限不足造成的。这也可能是由于未启用角色管理器功能所致。单击下面的按钮重定向到一个页面,您可以在其中选择新的数据存储。
以下消息可能有助于诊断问题: 无法连接到 SQL Server 数据库。
I've verified that my credentials are correct (I can use them to connect via SQL Management Studio). Is there anything else I can check? I'm stumped.
我已经验证我的凭据是正确的(我可以使用它们通过 SQL Management Studio 进行连接)。还有什么我可以检查的吗?我难住了。
UPDATE:
更新:
I wasn't able to connect to my default instance from within SQL Management Studio (MSSQLSERVER) so I reinstalled SQL, creating a named instance (LHSQLSERVER). Now I'm able to connect to that instance in SQL Management Studio, but I'm still getting the same error from the ASP.NET Configuration.
我无法从 SQL Management Studio (MSSQLSERVER) 中连接到我的默认实例,因此我重新安装了 SQL,创建了一个命名实例 (LHSQLSERVER)。现在我可以在 SQL Management Studio 中连接到该实例,但是我仍然从 ASP.NET 配置中收到相同的错误。
Another thing to note - the aspnet_regsql tool I ran was from the Framework64\v4.0.30319 folder. Is that correct if I am using .NET 4.5?
另一件需要注意的事情 - 我运行的 aspnet_regsql 工具来自 Framework64\v4.0.30319 文件夹。如果我使用 .NET 4.5,那是否正确?
UPDATE 2:
更新 2:
I've tried replacing my connection string with a connection string to a remote site (i.e. mysite.winhost.com) that I know works, but I'm still getting the same error in the ASP.NET Website Configuration Tool? FWIW I'm also using Windows 8, but I didn't think that would matter.
我已经尝试将我的连接字符串替换为我知道有效的远程站点(即 mysite.winhost.com)的连接字符串,但我仍然在 ASP.NET 网站配置工具中遇到相同的错误?FWIW 我也在使用 Windows 8,但我认为这无关紧要。
Any thing else I can check?
还有什么我可以检查的吗?
UPDATE 3:
更新 3:
I found this postthat says you don't need the aspnet_regsql tool anymore for MVC 4, so I re-ran the tool removing all the settings, but again, no luck. Has anyone done this with MVC 4 before?
我发现这篇文章说你不再需要 MVC 4 的 aspnet_regsql 工具,所以我重新运行了该工具,删除了所有设置,但同样,没有运气。有没有人以前用 MVC 4 做过这个?
UPDATE 4:
更新 4:
See my answer below for the solution I found.
有关我找到的解决方案,请参阅下面的答案。
采纳答案by lhan
Thanks everyone for the input, I have finally found a solution. I came across this articlethat describes the new Simple Membership that VS 2012 uses for MVC 4. I basically just started over, creating a new database, new MVC 4 project, and just followed the instructions in the article. Sure enough, just "registering" on the web page created the table(s) in my database and stored the user information.
谢谢大家的意见,我终于找到了解决方案。我看到了这篇文章,它描述了 VS 2012 用于 MVC 4 的新简单成员资格。我基本上只是重新开始,创建一个新数据库、新 MVC 4 项目,然后只是按照文章中的说明进行操作。果然,只需在网页上“注册”即可在我的数据库中创建表并存储用户信息。
回答by manuel
didn't try with SQL 2012 but this Connection string worked fine with SQL 2008 R2
没有尝试使用 SQL 2012,但此连接字符串与 SQL 2008 R2 配合良好
connectionString="Server=ServerAddress;Database=DataBaseName;User Id=Username;Password=Password;"
or
或者
connectionString="user id=UserName;password=Password;initial catalog=DatabaseName;data source=SQLServerIPorFQDN;Connect Timeout=30;"
回答by Parag Meshram
If you are able to connect using Sql Management Studio then I believe there must be problem with Connection String -
如果您能够使用 Sql Management Studio 进行连接,那么我相信连接字符串一定有问题 -
Connection String You are using follows format -
连接字符串您正在使用以下格式 -
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;
Try using IP Address, Portin Data Source field like -
尝试IP Address, Port在数据源字段中使用-
<connectionStrings>
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=190.190.200.100,1433;
Initial Catalog=MyDatabaseName;
Integrated Security=False;
User ID=MyUserID;Password=MyPassword;
MultipleActiveResultSets=True" />
</connectionStrings>
OR try other variations -
或尝试其他变体 -
Standard Security
标准安全
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
Trusted Connection
可信连接
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Connection to a SQL Server instance
连接到 SQL Server 实例
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
Reference - http://www.connectionstrings.com/sql-server-2012
回答by Prabhu Murthy
You may need to enable the role manager in config
您可能需要在配置中启用角色管理器
<roleManager enabled="true"/>
回答by Glenn Gordon
While lhan16's answer helped me, it would have been simpler to say that all you needed to add was a DefaultConnection. I started from the same place, and the database connection that the MVC uses doesn't work for the security framework. The authors of the framework never imagined that you would not use the DefaultConnection.
虽然 lhan16 的回答对我有帮助,但说您需要添加的只是一个 DefaultConnection 会更简单。我从同一个地方开始,MVC 使用的数据库连接不适用于安全框架。该框架的作者从未想过您不会使用 DefaultConnection。

