简体   繁体   English

将参数发送到 Rest API 用于 SQL 服务器

[英]Sending parameters to Rest API for SQL Server

I have created a SQL server with a table that has data in on a server I have local access to.我已经创建了一个 SQL 服务器,其中一个表在我可以本地访问的服务器上包含数据。 I have also created an "API" on that same server to get the information from the SQL server so it can be read by my angular application.我还在同一台服务器上创建了一个“API”来从 SQL 服务器获取信息,以便我的 angular 应用程序可以读取它。

At the moment, I can read the rows in the SQL server and have them displayed in my angular application, but, I want to be able to update the SQL table from my angular app (through the API). At the moment, I can read the rows in the SQL server and have them displayed in my angular application, but, I want to be able to update the SQL table from my angular app (through the API).

This is my API ( sqlserver.js ):这是我的 API ( sqlserver.js ):

var express = require('express');
var app = express();
var cors = require('cors');

app.use(cors())

app.get('/', function (req, res) {

    var sql = require("msnodesqlv8");

    // config for your database
    var config = "server=servername;Database=dataBaseName;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
    const query = "SELECT * FROM tableName";
    // connect to your database

    sql.query(config, query, (err, rows) => {
        res.send(rows)
     });
});


var server = app.listen(3097, function () {
    console.log('Server is running..');
});

I want to be able to use the query我希望能够使用查询

const query = "SELECT * from tableName WHERE ID LIKE <inputFromAngular>"

But I am not sure how to get the parameter from angular into the sqlserver.js .但我不确定如何将参数从 angular 获取到sqlserver.js中。 (If I can do this then it will lead to updating the SQL Table using SET (如果我能做到这一点,那么它将导致使用SET更新 SQL 表

In my angular app this is how I am calling the sqlserver.js to display the SQL table:在我的 angular 应用程序中,这就是我调用sqlserver.js以显示 SQL 表的方式:

import { HttpClient } from '@angular/common/http';

constructor(
    private httpService: HttpClient
    ) { }


this.httpService.get("http://servername:3097").subscribe(response => {
        console.log(response)
        this.sqlData = response;
      })

I have tried using this.httpService.post() but I wasn't sure how to get the parameters in the API?我尝试过使用this.httpService.post()但我不确定如何获取 API 中的参数?

You will need to change you get method to send params from ui side and get params on backend side so add a server side method like您将需要更改获取方法以从 ui 端发送参数并在后端获取参数,因此添加一个服务器端方法,例如

app.get('/:id', function (req, res) {

    var sql = require("msnodesqlv8");
 var id = req.params.id,
    // config for your database
    var config = "server=servername;Database=dataBaseName;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
    const query = "SELECT * FROM tableName WHERE ID LIKE "+id;
    // connect to your database

    sql.query(config, query, (err, rows) => {
        res.send(rows)
     });
});

then on angular side然后在 angular 侧

this.httpService.get("http://servername:3097/"+yourid).subscribe(response => {
        console.log(response)
        this.sqlData = response;
      })

Also I would suggest you to create a root like /yourapiname/:id instead of /:id and url on angular side should be " http://servername:3097/yourapiname "+yourid because your current root can result to confliction. Also I would suggest you to create a root like /yourapiname/:id instead of /:id and url on angular side should be " http://servername:3097/yourapiname "+yourid because your current root can result to confliction.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM