简体   繁体   English

如何连接到现有数据库? .NET Core API和SQL Server 2012

[英]How to connect to existing database? .NET Core API & SQL Server 2012

Good day! 美好的一天!

I am struggling on how to connect existing database with existing data to my API project. 我正在努力将现有数据库与现有数据连接到我的API项目。 And also how do I know if I'm already connected? 还有如何知道我是否已经连接? I am using .NET Core 1.1 and my database is SQL Server 2012. 我正在使用.NET Core 1.1,而我的数据库是SQL Server 2012。

在此处输入图片说明

appsettings.json : appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
    "ConnectionStrings": {
      "DefaultConnection": "Server=.\\SQLEXPRESS;Database=;User Id=sa;Password=Passw0rd;MultipleActiveResultSets=True"
    }
  }
}

Startup.cs - ConfigureServices Startup.cs-ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    services.AddDbContext<DefaultConnection>(
    options => options.UseSqlServer(
    Configuration.GetConnectionString("DefaultConnection")));
}

I am following this tutorial but it seems that this creates new database. 我正在关注本教程,但看来这会创建新的数据库。 http://www.mithunvp.com/aspnet-core-web-api-entity-framework-core/ http://www.mithunvp.com/aspnet-core-web-api-entity-framework-core/

Would highly appreciate all your comments/solutions/suggestions. 非常感谢您的所有评论/解决方案/建议。

Thank you very much! 非常感谢你!

You need to add the name of the database into that line : 您需要在该行中添加数据库名称:

 "DefaultConnection": "Server=COGSOL-LAP13;Database=DATABASENAME;UserId=sa;Password=Passw0rd;MultipleActiveResultSets=True"

And as CodeCaster comment says, you should use the name of the server too 正如CodeCaster的评论所说,您也应该使用服务器的名称

You also have a space between User and Id User Id should be 用户和ID之间也应有一个空格User Id应该为

UserId 用户身份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用托管标识将现有的.Net Core 应用程序连接到 Azure SQL 数据库 - How to connect an already existing .Net Core application to Azure SQL Database with managed identity 如何在VS Express 2012 for Web中连接到SQL Server数据库 - How to connect to SQL Server database in VS Express 2012 for Web .NET Core 无法连接到远程 SQL Server 数据库 - .NET Core can't connect to remote SQL Server database 如何通过网络连接到SQL Server数据库? - How to connect to a sql server database over the net? 如何将 ASP.NET Core 5 MVC 项目连接到 Supabase 数据库而不是 SQL 服务器? - How to connect ASP.NET Core 5 MVC project to Supabase database instead of SQL Server? 使用C#连接到SQL Server 2012数据库(Visual Studio 2012) - Connect to SQL Server 2012 Database with C# (Visual Studio 2012) 如何使用ASP.NET Core API与Microsoft SQL Server连接 - How to connect with microsoft sql sever with ASP.NET Core API 如何从托管在 **1&amp;1 Ionos** 托管服务器中的 ASP.NET Core 2.2 连接 MS SQL Server 数据库? - How to connect MS SQL Server database from ASP.NET Core 2.2 hosted in **1&1 Ionos** hosting server? 如何使用ASP Net Core 2连接到SQL Server 2008? - How to connect to SQL Server 2008 with asp net core 2? .NET 核心 - 无法连接 SQL 服务器 - .NET Core - not able to connect SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM