MVC 4与SQLServer 2012:如何实现Connectionstring ?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (229)

我使用VisualStudio 2012在C#中创建了一个全新的MVC 4应用程序。我试图连接到一个全新的SQL Server 2012(Standard)实例,但似乎无法正确设置连接字符串。

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

每次我从VisualStudio中转到ASP.NET配置时,页面都会加载,但只要单击“Security”,就会收到以下消息:

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.

我已经验证了我的凭据是正确的(我可以使用它们通过SQLManagementStudio进行连接)。怎么解决?

提问于
用户回答回答于

没有尝试使用SQL 2012,但此连接字符串在SQL2008R2中运行良好

connectionString="Server=ServerAddress;Database=DataBaseName;User Id=Username;Password=Password;"

connectionString="user id=UserName;password=Password;initial catalog=DatabaseName;data source=SQLServerIPorFQDN;Connect Timeout=30;"
用户回答回答于

如果您能够使用SQLManagementStudio进行连接,您使用的连接字符串格式如下:

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;

试着在数据源字段中使用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>

标准安全

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

可信连接

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

与SQLServer实例的连接

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;

参考资料:http://www.connectionstrings.com/sql-server-2012

扫码关注云+社区

领取腾讯云代金券