我已经连接MSSQL使用下面的代码全球连接已存在问题的node.js
var sql = require('mssql')
var config ={
server:'xxxx',
database:'xxxx',
user:'xxx',
password:'xxx',
port:'xx'
};
sql.connect(dbconfig, function (err) {
if (err) console.log(err);
var sqlquery='';
const request = new sql.Request();
if(condition)
{
//query to the database and get the repo value
sqlquery='select * from verylargetable';
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
var repo=result.recordset[0].Repo;
//query to the database and get the comm value
sqlquery="select commit from verylargetable where Repo='"+repo+"'";
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
var comm=result.recordset[0].Comm;
if (result.recordset.length > 0)
{
//query to the database and update the table
sqlquery="UPDATE verylargetable set Repo='"+repo+"', WHERE Comm='"+comm+"'";
request.query(sqlquery, function (err,result){
if (err) console.log(err)
console.log("record(s) updated");
});
}
});
});
}
else
{
//query to the database and get the repo value
sqlquery='select * from verylargetable';
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
var repo=result.recordset[0].Repo;
//query to the databaseto insert new record
sqlquery ="INSERT INTO verylargetable VALUES("+repo+"','"+comm+"',1)";
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
});
});
}
});
的node.js基于某些条件必须执行查询。在执行时,这些查询会被正确更新。但有时会遇到以下问题
全局连接已经存在。首先调用sql.close()。
当我在最后使用sql.close()时,我无法下次初始化连接。
面向连接关闭问题。
我有单独的数据库更改方法。我需要在方法开始时建立连接,并且必须在最后关闭。在两者之间必须执行所有查询。 请让我知道如何正确打开和关闭连接?
可能有[错误:全局连接已存在。调用sql.close()第一](https://stackoverflow.com/questions/43345149/error-global-connection-already-exists-call-sql-close-first) –
https://stackoverflow.com/questions/ 43345149/error-global-connection-already-exists-call-sql-close-first –
你试过用下面的解决方案吗? –