-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql.js
43 lines (36 loc) · 1.63 KB
/
mssql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
let defaultconfig = {
userName: 'developer',
password: 'devel0per-user',
server:'',
options: {database: 'test',}
};
exports.mssql = function(config){
this.connection = new Connection(config);
this.query = function(str,callback){ //执行查询
var connection = this.connection;
var rows={};
connection.on('connect', function(err){ //连接数据库,执行匿名函数
if(err){
console.log('connection err',err)
callback({'err':err['message']+'请检查账号、密码是否正确,且数据库存在'});
}else{
var request = new Request(str,function(err, rowCount){
if(err)err = {'err':err['message']};
callback(err,rows);
connection.close();
});
var n=0;
request.on('row', function(columns) { //查询成功数据返回
rows[n]={};
columns.forEach(function(column) {
rows[n][column.metadata.colName] = column.value; //获取数据
});
n++;
});
connection.execSql(request); //执行sql语句
}
});
}
}