13

I am trying to connect my ASP.NET Core application to my database.

To do this I'm supposed to edit the appsettings.json file and define my default connection.

I am not sure what to put under Server and Database.

I am trying to connect the database StudentDB.

Here is what string I tried to use to connect to my database:

enter image description here

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=ACTKPTP115;Database=StudentsDB;Trusted_Connection=True;MultipleActiveRes ultSets=true;"
},

As you can see I connected the database in Visual Studio.

Here is the database I'm trying to connect in Microsoft SQL management studio:

enter image description here

What do I type in the ConnectionStrings under Server and Database to properly connect my server?

3 Answers 3

20

Can you try:

Server=ACTKPTP115\\SQLTEST;Database=StudentsDB;Trusted_Conn‌ection=True;Multiple‌​ActiveResultSets=tru‌​e;

Some resource from Microsoft about connection strings for ASP.NET Core: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

2
  • That was it but it's actually "Server=ACTKPTP115\\SQLTEST;Database=StudentsDB;Trusted_Connection=True;MultipleActiveResultSets=true;"
    – ire
    Commented Mar 21, 2017 at 9:14
  • Cool, thanks for pointing that mistake out. Edited the answer to reflect correctly for those who references it in the future. Have a good one!
    – ngobw
    Commented Mar 21, 2017 at 9:47
20

This piece of code worked for me:

{
"ConnectionStrings": {
    "DefaultConnection": "Server=tcp:xxUAT.domain.com,64609;Initial Catalog=IdentityDB;MultipleActiveResultSets=true;User ID=xx;Password=xx"
} }
0

This is my connection string when I want to connect to sql server database on my local machine:

"server=.; database=MyDB; Integrated Security=True;TrustServerCertificate=True; User Id=MyUserName; Password=123;"

AppSettings.Development.json:

  "ConnectionStrings": {
    "MyDbConnectionString": "server=.; database=MyDB; Integrated Security=True;TrustServerCertificate=True; User Id=MyUserName; Password=123;"
  }

and program.cs file :

builder.Services.AddDbContext<MyDbContext>(
    dbContextOptions => dbContextOptions.UseSqlServer(
        builder.Configuration["ConnectionStrings:MyDbConnectionString"])
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.