0

How to write the connection string to local SQL Server database using web.config?

Here is my connection string in web.config. But when I open local/demo, it said:

login failed for user ''.

Actually I'm using Windows authentication, so I thought it doesn't need user and password.

<connectionStrings>
    <add name="HRISContext" 
         connectionString="data source=.;initial catalog=BPSDEMO;Pooling=false;persist security info=True;user id=;password=;MultipleActiveResultSets=True;App=EntityFramework" 
         providerName="System.Data.SqlClient"  />
</connectionStrings>
5
  • You need to add Integrated Security in connectionstring. <add name="HRISContext" connectionString="data source=.;initial catalog=BPSDEMO;Pooling=false;persist security info=True;Integrated Security=SSPI;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> For more information check connectionstrings.com/sqlconnection Commented Sep 1, 2018 at 16:04
  • I already added the integrated security. Then the error: 'Login failed for user 'IIS APPPOOL\DefaultAppPool'. @MohsinMehmood Commented Sep 1, 2018 at 16:16
  • It indicates that your application is trying to connect to SQL database using Application pool identity. One way to fix is to add IIS AppPool\DefaultAppPool user to database. Checkout this SO answer for step by step instructions: stackoverflow.com/questions/7698286/… Commented Sep 1, 2018 at 16:32
  • How about this? <add name="HRISContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\BPSDEMO.mdf;Initial Catalog=BPSDEMO;Integrated Security=True;MultipleActiveResultSets=True" /> The error message: 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 - @MohsinMehmood Commented Sep 1, 2018 at 16:35
  • This will only work if you have your database mdf file inside App_Data folder. Basically, this option allows you to deploy your database file along your project. Commented Sep 1, 2018 at 16:54

1 Answer 1

1

If you want to use Windows authentication, then yes, you must not include any user id and pwd in your connection string - just remove those completely. But you must include the Integrated Security=SSPI setting instead.

Try this:

<connectionStrings>
    <add name="HRISContext" 
         connectionString="data source=.;initial catalog=BPSDEMO;Integrated Security=SSPI;MultipleActiveResultSets=True;Pooling=false;persist security info=True;App=EntityFramework" 
         providerName="System.Data.SqlClient"  />
</connectionStrings>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.