使用 Express 和 Node Js 时 SQL Server 连接问题

问题描述 投票:0回答:0

我一直在尝试使用node.js连接到SQL Server。我正在使用 Express,但无法连接到我的数据库。起初我遇到了有关 ISS 的问题,我能够解决它,但现在连接是问题。

const express = require('express');
const app = express();
const mssql = require("mssql");

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

    // Config your database credential
    const config = {
        user: 'sa',
        password: '12345678',
        server: 'localhost',
        database: 'student'
    };

    // Connect to your database
    mssql.connect(config, function (err) {

        // Create Request object to perform
        // query operation
        let request = new mssql.Request();

        // Query to the database and get the records
        request.query('select * from dbo.student',
            function (err, records) {

                if (err) console.log(err)

                // Send records as a response
                // to browser
                res.send(records);

            });
    });
});

let server = app.listen(5000, function () {
    console.log('Server is listening at port 5000...');
});

错误:

RequestError:没有为该请求指定连接。 在 Request._query (C:\User

javascript node.js sql-server database-connection
© www.soinside.com 2019 - 2024. All rights reserved.