nodejs連線sqlserver遇到的問題

2021-08-14 19:20:23 字數 4437 閱讀 8581

首先,我用了mssqlheper模組,但是死活連不上資料庫,於是放棄,選擇了mssql模組。

**如下:

var mssql = require('mssql');  

var db = {};

var config = ;server=#\\sql;database=#;uid=#;pwd=#;",

/* options: ,*/

pool:

}; db.sql = function (sql, callback)

var ps = new mssql.preparedstatement(connection);

ps.prepare(sql, function (err)

ps.execute('', function (err, result)

ps.unprepare(function (err)

callback(err, result);

});

});

});

});

};

module.exports = db;

var express = require('express');  

var db = require('./db');

const request = require('request-json');

var client = request.createclient('');

var lastid = 0;

var timebw = 1 * 1000;

class postdata

else

// 當讀取到最後一條且大於50條時,lastid減50。

if (lastid = lastid && lastid > 50) });

}_postdate(item, fc) ;

postdata['creator_id'] = item['\u5e8f\u53f7'];

postdata['weight_id'] = 'code002';

postdata['weight_code'] = 'code002';

postdata['weight_rfid'] = '0';

postdata['veh_id'] = item['\u6d41\u6c34\u53f7'];

postdata['car_no'] = item['\u8f66\u53f7'];

postdata['weight_gross'] = item['\u6bdb\u91cd'];

postdata['weight_veh'] = item['\u76ae\u91cd'];

postdata['weight_net'] = item['\u51c0\u91cd'];

postdata['garbage_type'] = item['\u8d27\u540d'];

postdata['weight_date'] = item['\u76ae\u91cd\u65f6\u95f4'];

postdata['delivery_unit'] = item['\u53d1\u8d27\u5355\u4f4d'];

postdata['prj_id'] = 2;

postdata['tcar_company_id'] = 0;

postdata['creator_id'] = item['\u5e8f\u53f7'];

console.log('post data', postdata);

client.post('/jsonapi/cancu_weight/add.json',

postdata, function (err, res, body)

else );

}});

}_time(fc) );

return;

}_self._postdate(d[0], fc);})}

run(fc) , timebw);

}, timebw)

}}var t = new postdata();

t.run();

結果報錯如下:

無力解決,只好在網上查詢其他的方法,終於又找到了tedious模組,**如下:

const connection = require('tedious').connection;    

const request = require('tedious').request;

exports.mssql = function(config);

connection.on('connect', function(err));

}else;

callback(err,rows);

// connection.close();

});

var n=0;

request.on('row', function(columns) ;

columns.foreach(function(column) );

n++;

});

connection.execsql(request); //執行sql語句

}

});

}

}

const request = require('request-json');

var client = request.createclient('');

var mssql = require('./tedious.js');

var conn = new mssql.mssql( });

var lastid = 0;

var timebw = 1 * 1000;

class postdata

else });

}_postdate(item, fc) ;

postdata['creator_id'] = item['\u5e8f\u53f7'];

postdata['weight_id'] = 'code002';

postdata['weight_code'] = 'code002';

postdata['weight_rfid'] = '0';

postdata['veh_id'] = item['\u6d41\u6c34\u53f7'];

postdata['car_no'] = item['\u8f66\u53f7'];

postdata['weight_gross'] = item['\u6bdb\u91cd'];

postdata['weight_veh'] = item['\u76ae\u91cd'];

postdata['weight_net'] = item['\u51c0\u91cd'];

postdata['garbage_type'] = item['\u8d27\u540d'];

postdata['weight_date'] = item['\u76ae\u91cd\u65f6\u95f4'];

postdata['delivery_unit'] = item['\u53d1\u8d27\u5355\u4f4d'];

postdata['prj_id'] = 2;

postdata['tcar_company_id'] = 0;

postdata['creator_id'] = item['\u5e8f\u53f7'];

console.log('post data', postdata);

client.post('/jsonapi/cancu_weight/add.json',

postdata, function (err, res, body)

else );

}});

}_time(fc) );

return;

}_self._postdate(d[0], fc);})}

run(fc) , timebw);

}, timebw)

}}var t = new postdata();

t.run();

結果執行一次sql沒問題,第二次就卡在執行sql的地方:

找不到什麼原因,接下來會繼續找原因

localhost無法連線sqlserver

因為工作關係我的電腦安裝了2012 2017兩個版本的sqlserver,之後把2017的解除安裝了,出現了vs開啟專案不報錯,一直在轉圈的問題,之後試過本機登入sqlserver 發現登不上,要指定例項才可以。開啟配置管理器 1.開啟sqlserver網路配置,點選例項協議 2.雙擊tcp ip ...

資料庫連線池 Oracle和SQL Server

近期看到許多朋友在問資料庫連線池的問題,所以特地總結了一下.以下是oracle和sql server在配置連線池時的區別 1.oracle與sql server在配置連線池時的區別 tomcat配置檔案中 oracle的配置方法 urljdbc oracle thin 172.28.122.49 1...

nodejs連線postgreSQL資料庫

nodejs連線pg資料庫有兩種方式,一種是直接連線 操作 斷開 還有一種是使用連線池,這種方式可以有效提公升多併發的效率 下邊是使用兩種不同方式的測試 var pgopt require pg 使用連線池 function connectpgwithpool var pgpool new pgop...