无法连接到另一台PC上的本地DB。 ASP .NET MVC4

时间:2014-09-22 07:19:01

标签: c# sql-server asp.net-mvc entity-framework asp.net-mvc-4

我在“PC1”上创建了一个mvc 4数据库应用程序。我刚刚通过右键单击“数据库SQL Server”选择并使用Database First方法创建实体框架。它在“PC1”上运行正常。但是当我将我的项目复制到另一台PC时,项目失败了。

我在PC上没有SQL Server。我刚刚用本地数据库复制了mvc 4项目。

错误是:“...(提供程序:SQL网络接口,错误:26 - 找到指定的服务器/实例时出错)”

我想我应该重写连接字符串。我已经完成了这个步骤: http://msdn.microsoft.com/en-us/library/hh873188.aspx 我试图改变连接字符串:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

要:

我已将PC(TomPC)的名称添加到连接字符串:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.TomPC\SQLEXPRESS;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.TomPC\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.TomPC\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
 </connectionStrings>

然而,它失败并出现同样的错误。  我非常感谢任何帮助!

3 个答案:

答案 0 :(得分:3)

您还需要确保在TomPC上的SQL Server配置中启用了TCP / IP,并且还打开了端口1433(默认端口,但它可能不同,例如,如果有多个实例在该PC上运行的SQL。)

答案 1 :(得分:1)

数据源=。\ SQLEXPRESS

。表示“localhost”或127.0.0.1

所以添加。表示localhost \ SQLEXPRESS或127.0.0.1 \ SQLEXPRESS

您需要将其更改为:

数据源= TomPC \ SQLEXPRESS

答案 2 :(得分:0)

我找到了答案。应用程序无法连接,因为连接字符串应修改为LocalDB:

 <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />    
 </connectionStrings>   

http://www.asp.net/mvc/tutorials/mvc-5/introduction/creating-a-connection-string