我正在尝试在nodejs项目和运行Microsoft sql Server 2005的服务器之间建立连接.我使用的是节点模块mssql,但是当我尝试创建连接时出现这些错误:
{ [ConnectionError: Failed to connect to 123.123.12.1:1433 in 15000ms]
name: ‘ConnectionError’,
message: ‘Failed to connect to 123.123.12.1:1433 in 15000ms’,
code: ‘ETIMEOUT’ }
我的联系是由
var sql = require('mssql'); var dbConfig = { server:'123.123.12.1',database:'testingDB',user:'userName',password:'pass',port:1433 }; function getEmp() { var conn = new sql.Connection(dbConfig); var req = new sql.Request(conn); conn.connect(function(err) { if(err) { console.log(err); return; } else { console.log('success'); } }); } getEmp();
我不确定我做错了什么,如果有帮助我使用的是云端9 IDE.
解决方法
把你的var req = new sql.Request(conn)放在connect中.
// config for your database var config = { user: 'sa',password: 'mypassword',server: 'localhost',database: 'SchoolDB' }; // 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 Student',function (err,recordset) { if (err) console.log(err) // send records as a response res.send(recordset); }); });