Error Connecting to DB.mdf when published IIS

2019-09-20 09:44发布

问题:

I have been searching all over different sources in search on solving my issue.. I've published a website on IIS that I have been working on and this is my first published website on IIS.

I seem to not be able to connect to the database.

I will provide everything that I have done right down here..

Appreciate any help please....

My web.config file in the published website directory C:\FinancialHubTest. On a side note I also tried adding User Instance=True to my connection string in my web.config file but it doesn't work..

<connectionStrings>
    <!--<add name="ConnStr" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\FinancialHub.mdf;Integrated Security=True" />-->
    <add name="ConnStr" 
         connectionString="Data Source=(LocalDB)\.\IIS_DB;Integrated Security=True;" />
</connectionStrings>

Details of MSSQLLocalDB from cmd

applicationHost file in C:\Windows\System32\inetsrv\config edited with Notepad2. I'm using FinancialHub as my applicationPool for my published website in IIS

<applicationPools>
        <add name="DefaultAppPool" managedRuntimeVersion="v4.0" />
        <add name="Classic .NET AppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
        <add name=".NET v2.0 Classic" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
        <add name=".NET v2.0" managedRuntimeVersion="v2.0" />
        <add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" />
        <add name=".NET v4.5" managedRuntimeVersion="v4.0" />
        <add name="FinancialHub" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated">
          <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
        </add>
        <applicationPoolDefaults managedRuntimeVersion="v4.0">
            <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
        </applicationPoolDefaults>
    </applicationPools>

My Published Website directory with App_Data having the FinancialHub.mdf file

My SQL Server Object Explorer, the connection only appears when I run my website via Visual Studio 2015 I think that would be running on IIS Express..

I have been getting this error every time I change here and there but I'm really sure why.. Or rather what changes I make to either web.config, etc will cause this error. I check the windows event log too.

I also followed these 2 links on dealing with this issue,

Using LocalDB with Full IIS, Part 1: User Profile,

Using LocalDB with Full IIS, Part 2: Instance Ownership

If there is a need for more information i can provide..

I feel like i have been beating around the bush trying to figure out but i can't seem to solve it..

I really do appreciate any help please. Thank you so much.

回答1:

You are still using LocalDB even after publishing the application to IIS. LocalDB (SQL Database inside App_Data) is primarily intended for developing and testing purpose only.

You need a real SQL Server when you host the site on IIS. So you need to install SQL Server either in the same server that the application is hosted or a dedicated SQL server. Then attach or copy the existing database to SQL Server.