将数据从nodejs控制台移动到SQL Server

时间:2022-03-13 15:40:23

I am very new to node.js. Using the following code I am able to retrive data from the wesbite intrino into console.

我对node.js很新。使用以下代码,我能够将wesbite intrino中的数据转换为控制台。

var https = require("https");

var username = "********";
var password = "********";
var auth = "Basic " + new Buffer(username + ':' + 
password).toString('base64');

var request = https.request({
method: "GET",
host: "api.intrinio.com",
path: "/companies?ticker=AAPL",
headers: {
    "Authorization": auth
}
}, function (response) {
var json = "";
response.on('data', function (chunk) {
    json += chunk;
});
response.on('end', function () {
    var company = JSON.parse(json);
    console.log(company);
});
});

request.end();

And the result is as follows:-

结果如下: -

将数据从nodejs控制台移动到SQL Server

My question is: how do I transfer this data into SQL Server? I tried watching a few tutorials and videos. But I was not able to exactly understand how it works.

我的问题是:如何将此数据传输到SQL Server?我试着看一些教程和视频。但我无法完全理解它是如何工作的。

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

This is a very broad question. Connecting MS-SQL server from node usually uses tedious package. You can directly use this package to insert data as described in https://docs.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js

这是一个非常广泛的问题。从节点连接MS-SQL服务器通常使用繁琐的包。您可以直接使用此程序包插入数据,如https://docs.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-中所述使用节点-JS

Following is a snippet from the above link.

以下是上述链接的摘录。

var Connection = require('tedious').Connection;  
    var config = {  
        userName: 'yourusername',  
        password: 'yourpassword',  
        server: 'yourserver.database.windows.net',  
        // If you are on Azure SQL Database, you need these next options.  
        options: {encrypt: true, database: 'AdventureWorks'}  
    };  
    var connection = new Connection(config);  
    connection.on('connect', function(err) {  
        // If no error, then good to proceed.  
        console.log("Connected");  
        executeStatement1();  
    });  

    var Request = require('tedious').Request  
    var TYPES = require('tedious').TYPES;  

    function executeStatement1() {  
        request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP);", function(err) {  
         if (err) {  
            console.log(err);}  
        });  
        request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');  
        request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');  
        request.addParameter('Cost', TYPES.Int, 11);  
        request.addParameter('Price', TYPES.Int,11);  
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                console.log('NULL');  
              } else {  
                console.log("Product id of inserted item is " + column.value);  
              }  
            });  
        });       
        connection.execSql(request);  
    }

But using an ORM is the best practice. sequelize is one of the best in the node community.

但使用ORM是最佳做法。 sequelize是节点社区中最好的之一。

$ npm install --save sequelize
$ npm install --save tedious // MSSQL

const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mssql'
});

You must go through either of these modules documentation to understand better.

您必须仔细阅读这些模块文档才能更好地理解。

#1


0  

This is a very broad question. Connecting MS-SQL server from node usually uses tedious package. You can directly use this package to insert data as described in https://docs.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js

这是一个非常广泛的问题。从节点连接MS-SQL服务器通常使用繁琐的包。您可以直接使用此程序包插入数据,如https://docs.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-中所述使用节点-JS

Following is a snippet from the above link.

以下是上述链接的摘录。

var Connection = require('tedious').Connection;  
    var config = {  
        userName: 'yourusername',  
        password: 'yourpassword',  
        server: 'yourserver.database.windows.net',  
        // If you are on Azure SQL Database, you need these next options.  
        options: {encrypt: true, database: 'AdventureWorks'}  
    };  
    var connection = new Connection(config);  
    connection.on('connect', function(err) {  
        // If no error, then good to proceed.  
        console.log("Connected");  
        executeStatement1();  
    });  

    var Request = require('tedious').Request  
    var TYPES = require('tedious').TYPES;  

    function executeStatement1() {  
        request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP);", function(err) {  
         if (err) {  
            console.log(err);}  
        });  
        request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');  
        request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');  
        request.addParameter('Cost', TYPES.Int, 11);  
        request.addParameter('Price', TYPES.Int,11);  
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                console.log('NULL');  
              } else {  
                console.log("Product id of inserted item is " + column.value);  
              }  
            });  
        });       
        connection.execSql(request);  
    }

But using an ORM is the best practice. sequelize is one of the best in the node community.

但使用ORM是最佳做法。 sequelize是节点社区中最好的之一。

$ npm install --save sequelize
$ npm install --save tedious // MSSQL

const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mssql'
});

You must go through either of these modules documentation to understand better.

您必须仔细阅读这些模块文档才能更好地理解。