Sql server 无法使用节点js连接到ms sql窗口身份验证

Sql server 无法使用节点js连接到ms sql窗口身份验证,sql-server,node.js,Sql Server,Node.js,这是我的节点js代码 app.get('/', function (req, res) { var config = { server: 'localhost\\SQLEXPRESS2008', database: 'TestingDB' }; // connect to your database sql.connect(config, function (err) { if (err) console.log(err); // create Reques

这是我的节点js代码

app.get('/', function (req, res) {
var config = {
    server: 'localhost\\SQLEXPRESS2008',
    database: 'TestingDB'
};

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

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

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

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

        console.log(recordset);
    });
});
}))

请建议我在命令提示符下运行此命令后连接sql并获取此错误

服务器正在运行。。在端口8020上

{错误:无法连接到本地主机:15000ms内未定义

    at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:293:19)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:191:7)
    at Connection.connectTimeout (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:795:12)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)
  code: 'ETIMEOUT',
  originalError:
   { ConnectionError: Failed to connect to localhost:undefined in 15000ms
       at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
       at Connection.connectTimeout (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:795:28)
       at ontimeout (timers.js:386:14)
       at tryOnTimeout (timers.js:250:5)
       at Timer.listOnTimeout (timers.js:214:5)
     message: 'Failed to connect to localhost:undefined in 15000ms',
     code: 'ETIMEOUT' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
    at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
    at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
    at D:\Nodejs\UsersCreate\app.js:118:17
    at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
undefined
这可能有助于:

  • 在Windows服务中启动“SQL SERVER BROWSER”服务(我已将其配置为自动启动)

  • 允许SQL Server Express通过TCP/IP为端口1433接受远程连接:

  • 最后重新启动“SQL Server”和“SQL Browser Agent”服务

    安装mssql软件包时,我遇到了类似的问题,并在配置中添加了一个实例和数据库名称

    我认为最好在db上创建一个用户

    在我需要处理错误后,将try catch添加到我的函数,该函数将在db中连接,并在函数出现错误时关闭连接

    所有数据库连接都位于不同的文件中,因此我导出要在我的应用程序中使用的模块,如果需要在其他应用程序中使用,只需添加对该模块的引用

    let QueryDB = {
        Query_SelectAll: 'SELECT [COLUMN] \
                                    , [COLUMN] \
                                    , [COLUMN] \
                    FROM [DATABASE].[dbo].[TABLE_NAME]',
    
        Query_Delete: 'DELETE FROM [DATABASE].[dbo].[TABLE_NAME] WHERE COLUMN = '
    };
    
    我将所有查询放在一个对象中,以简化对我的使用

    let ConnectionString = {
        user: 'YOUR_DBUSER',
        password: '****',
        server: '0.0.000.00',
        options: {
            instance: 'YOURINSTANCE',
            database: 'DATABASE_NAME'
        }
    };
    
    在server属性中,在添加带有实例和数据库名称的选项之后,我放置了服务器IP

    function SQL_Server(Query, ret) {
        sql.close();
        sql.connect(ConnectionString).then(() => {
            console.log("Connected");
            return request(Query, ret);
        }).catch((error) => {
            console.log("Connect error");
            console.error(error);
            sql.close();
        });
    
    }
    
    function request(Query, ret) {
        let returning = undefined;
        let request = new sql.Request();
        (request.query(Query).then((recordset) => {
            sql.close();
            returning = recordset.recordset;
        }).catch((error) => {
            console.error(error);
            sql.close();
        })).then(() => {
            if (ret != undefined)
                ret(returning);
            console.log("Successful object return");
        });
    }
    
    这些是我的连接和请求功能

    module.exports.SQL_Server = SQL_Server;
    module.exports.QueryDB = QueryDB;
    
    因此,我导出模块以使用函数

    module.exports.SQL_Server = SQL_Server;
    module.exports.QueryDB = QueryDB;
    
    使用以下各项的示例:

    let DataBase = require("./Connection_Query");
    let FuncSql = DataBase.SQL_Server;
    let SQL = DataBase.QueryDB;
    
    APP.get(ProjectName + '/Home', (req, resp) => {
        FuncSql(SQL.Query_SelectAll, (rec) => {
            resp.render("Home", { YourObjects: rec });
        });
    });
    

    假设您使用这个答案来指导您。

    您必须在config.options.instanceName中编写数据库实例

    var config = {
        server: 'localhost\\SQLEXPRESS2008',
        database: 'TestingDB'
    };
    
    相反:

    const config = {
        user: '...',
        password: '...',
        server: 'localhost',
        database: 'TestingDB',
        options: {
            encrypt: false, // Use this if you're on Windows Azure
            instanceName: 'SQLEXPRESS2008'
        } 
    };
    
    你可能想看看这个。