向我的ASP.NET核心应用程序添加SQL连接 - 如何定义ConnectionStrings

时间:2017-03-21 08:36:59

标签: asp.net sql-server asp.net-core

我正在尝试将我的ASP.NET核心应用程序连接到我的数据库。

要做到这一点,我应该编辑appsettings.json文件并定义我的默认连接。

我不确定在服务器和数据库下放什么。

我正在尝试连接数据库StudentDB。

以下是我尝试用来连接数据库的字符串:

enter image description here

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

如您所见,我在Visual Studio中连接了数据库。

以下是我在Microsoft SQL管理工作室中尝试连接的数据库:

enter image description here

我在服务器和数据库下的ConnectionStrings中键入什么来正确连接我的服务器?

2 个答案:

答案 0 :(得分:8)

你可以尝试:

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

Microsoft的一些资源有关ASP.NET Core的连接字符串: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

答案 1 :(得分:5)

这段代码对我有用:

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