ASP.NET 核心与 Angular 7 模板连接到 SQL Server Express 数据库以读取 table

ASP.NET Core with Angular 7 template connect to SQL Server Express database to read table

我在 Visual Studio 社区 ASP.NET 核心中使用 Angular 模板创建了一个应用程序。我将项目的 Angular 升级到了 v7.

我尝试按照这个示例进行操作this c-sharpcorner link

问题是当我尝试使用 https://localhost:44367/api/Employee/Index 连接到服务器时,抛出了无法连接到服务器的异常。

SqlException: 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: 26 - Error Locating Server/Instance Specified)

所以我在SQL服务器中创建了table和程序,检查了它们的存在。 服务器已启动,并配置为 this msdn link.

我启用了 TCP/IP,并向防火墙添加了一条新规则以允许来自本地域的所有 TCP 端口请求。

我的Employee.cs改成了实际项目的名字

我的 EmployeeDataAccessLayer.cs 被修改为实际项目的名称和使用 ADO.Net 实体数据模型测试的连接字符串的名称。

我还将连接字符串添加到 web.config

我添加了控制器,我只更改了项目的名称。

在我的 Angular 应用程序 empservice.service.ts 中注入代码的服务中,我遇到了一些困难,因为我不知道修改下面的函数以使用 HttpClient (map ( rxjs) 在 Http 的 observable 上不起作用)。所以我在构造函数结束后立即注释了代码。

该服务已在应用程序中注册为提供商。

import { Injectable, Inject } from '@angular/core';
import { Http, Response} from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { Router } from '@angular/router';
import { map } from 'rxjs/operators';


@Injectable()
export class EmployeeService {
myAppUrl: string = "";
constructor(private httpClient: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.myAppUrl = baseUrl;
}
//getEmployees() {
//  return this.httpClient.get(this.myAppUrl + 'api/Employee/Index')
//        .map((response: Response) => response.json()
//        .catch(this.errorHandler));
//}

//getEmployeeById(id: number) {
//    return this._http.get(this.myAppUrl + "api/Employee/Details/" + id)
//        .map((response: Response) => response.json())
//        .catch(this.errorHandler)
//}

问题一 期望 https://localhost:44367/api/Employee/Index 给出 table 的内容是错误的吗? (控制器的实现还不够?) 问题2 你能帮我调整上面服务的代码以获取 Angular 应用程序中的数据吗?

基于错误:

SqlException: ... The server was not found or was not accessible. Verify that the instance name is correct ...

和连接字符串:

string connectionString = "data source = DESKTOP - RBLIODU\SQLEXPRESS;initial catalog = Employees; integrated security = True;MultipleActiveResultSets=True;App=EntityFramework";

我建议您将服务器名称更改为 (localhost)DESKTOP-RBLIODU(没有空格)。

像那样:

string connectionString = "data source=(localhost)\SQLEXPRESS;initial catalog = Employees; integrated security = True;MultipleActiveResultSets=True;App=EntityFramework";

祝你好运!

经过无数次尝试和失败后,回到最基本的东西,这是我没有学过的,因为我在学校做了其他事情,我在 post 中注意到要访问数据库,您必须先通过您的程序Visual studio,使用服务器资源管理器 连接到它。 由于我尝试使用的连接字符串未在 Visual Studio 中打开,因此我无法连接到数据库。

第二个问题,答案类似这样:

export class EmployeeService {
employees: Observable<Employees[]>;
newemployee: Employees;
constructor(private http: HttpClient) {
}
getEmployees() {
return this.http.get<Employee[]>(baseUrl + 'Employees');
}
etc...
}

我可能会编辑这个 post,在我掌握了这个过程之后。目前我的基本理解是这样。