【问题标题】:ASP.NET Core with Angular 7 template connect to SQL Server Express database to read table带有 Angular 7 模板的 ASP.NET Core 连接到 SQL Server Express 数据库以读取表
【发布时间】:2019-06-27 03:09:12
【问题描述】:

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

我试着按照这个例子this c-sharpcorner link

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

SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定实例)

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

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

我的Employee.cs被修改为实际项目的名称。

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

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

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

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

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

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)
//}

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

【问题讨论】:

  • 你能把代码限制在问题中需要的范围内吗?这样会更容易阅读和回答
  • 我尽可能地限制了代码。
  • 很高兴你包含了这么多细节,但在你的下一个问题上,你也可以尝试发布一个非常简洁的问题(例如错误和连接字符串)。这样,可能会有更多人阅读该问题,然后您可以根据人们的要求添加详细信息。
  • 非常感谢,你花时间给我建议真是太好了,但我把最初的帖子删了三分之一,我真的不知道该删什么。这样下一个有此任务的人不会像我一样浪费 4 天时间。

标签: c# sql-server asp.net-core .net-core ado.net


【解决方案1】:

根据错误:

SqlException: ... 未找到或无法访问服务器。 验证实例名称是否正确...

和连接字符串:

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";

祝你好运!

【讨论】:

    【解决方案2】:

    在无数次尝试和失败之后,回到最基本的东西,我没有学过,因为我在学校做过别的事情,我在一篇文章中注意到要访问数据库,你必须先连接到它通过你的程序,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...
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多