Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

c# - How to allow IIS to use local database from ASP.NET MVC project?

I'm going to be demoing a ASP.NET MVC website on a local network. This application has a connection to a database:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;AttachDbFilename=|DataDirectory|aspnet-EBC-20141127093222.mdf;Initial Catalog=aspnet-EBC-20141127093222;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

I would like if this database can be used by both IIS and whenever I run my application locally. I've made a site on IIS - it is running .NET v4. My project lives in c:inetpubwwwebc. I can publish the website but recieve this error upon viewing the page:


"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Unexpected error occurred inside a LocalDB instance API method call. See the Windows Application event log for error details."

I know I need to allow remote connections to the sql server through microsoft sql server manager? Is there a way to do it elsewhere?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best solution is to use SQL Expression but if you don't want there are few steps that you have to do to make your mvc application work with localdb

  1. Open Visual studio command prompt as administrator
  2. execute sqllocaldb share v11.0 IIS_DB (then the database will be shared and IIS can access it)
  3. Change the site connection string to point to the shared instance of DB: Data Source=(LocalDb).IIS_DB then publish again

Note that in this point you may receive an access deny error and there is how to fix it

  1. Go to visual studio => databaseexplorer => add connection
  2. For the server name enter (LocalDb).IIS_DB then right click on the connection and choose new query and execute this command

    create login [IIS APPPoolDefaultAPPPool] from windows;
    exec sp_addsrvrolemember N'IIS APPPoolDefaultAPPPool, sysadmin
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...