NODE.JS
Node.js - mssql server 연동
devLog by Ronnie's
2021. 6. 11. 23:30
Node.js - mssql server 연동
1. 커넥션 설정
var Connection = require('tedious').Connection;
var config = {
server: 'server ip',
authentication: {
type: 'default',
options: {
userName: 'your_username',
password: 'your_password'
}
},
options: {
// 에러 DeprecationWarning: Setting the TLS ServerName to an IP address is not permitted by RFC 6066. This will be ignored in a future version.Connected
encrypt: false,
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
connection.connect();
이때 tedious 모듈이 없다면 npm install tedious --save 명령어를 통해 설치한다.
2. 쿼리 실행
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
function executeStatement() {
request = new Request("query statement 입력;", function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
});
connection.execSql(request);
}