我正在尝试在nodejs项目和运行Microsoft SQL Server 2005的服务器之间建立连接。我正在使用节点模块mssql
,但在尝试创建连接时出现这些错误:
{[ConnectionError:无法连接到15000ms的123.123.12.1:1433]
name:'ConnectionError',
消息:'无法连接到15000ms的123.123.12.1:1433',
代码:'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。
答案 0 :(得分:11)
将var req = new 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);
});
});
答案 1 :(得分:6)
它对我有用。首先通过npm安装mssql( npm install --save mssql )
__iob_func
本地sql server的
var sql = require('mssql');
天蓝色服务器的
var sqlConfig = {
user: 'sa',
password: 'admin',
server: 'CBMOBILESHAMIM\\SQLEXPRESS',
database: 'databaseName'
};
现在连接到服务器
var sqlConfig = {
user: 'adminLogin',
password: 'admin',
server: 'severname.database.windows.net', // don't add tcp & port number
database: 'databaseName',
options: {
encrypt: true
}
};
有关详情,请查看mssql
答案 2 :(得分:0)
var webconfig = {
user: 'login',
password: 'sa@123',
server: 'localhost',
database: 'TestDB',
options: {
encrypt: false // Use this if you're on Windows Azure
}
}
var express = require('express');
var sql = require('mssql');
var http = require('http');
var connection = new sql.Connection(webconfig, function(err) {
var request = new sql.Request(connection);
request.query('select * from Users', function(err, recordset) {
if(err) // ... error checks
console.log('Database connection error');
console.dir("User Data: "+recordset);
});
});
var app = express();
var port = process.env.PORT || 8000;
或访问此处:https://nodejsbeginersprograms.blogspot.in/2017/02/nodejs-basic-tutorial-with-mssql.html