SQL Server与节点js的连接 [英] SQL Server connection to node js
问题描述
我正在尝试在nodejs项目和运行Microsoft SQL Server 2005的服务器之间建立连接.我正在使用节点模块 mssql
,但是在尝试创建连接时出现以下错误:
I am trying to establish a connection between nodejs project and server running Microsoft SQL Server 2005. I am using a node module mssql
, but I get these errors when I am attempting to create a connection:
{[ConnectionError:在15000毫秒内无法连接到123.123.12.1:1433]
名称:"ConnectionError",
消息:未能在15000毫秒内连接到123.123.12.1:1433",
代码:"ETIMEOUT"}
{ [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();
我不确定自己在做什么错,如果有帮助,我正在使用cloud 9 IDE.
I am not sure what I am doing wrong, I am using a cloud 9 IDE if that helps.
推荐答案
将您的 var req =新sql.Request(conn)
置于连接内部.
// 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);
});
});
这篇关于SQL Server与节点js的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!