所以这就是我创建连接的方式。请记住,config.mssql在我的config.js文件中,其中我有我需要使用的所有连接属性,如数据库用户名端口和密码
var connection = new sql.connect(config.mssql);
var request = new sql.Request(connection);
request.query('Insert into Campionamento_Contratto (Camp_Nome,Camp_Cognome,Camp_TelFisso,Camp_Cellulare,Camp_Note, Camp_UserInsert) values ('+req.body.campNome+','+req.body.campCognome+','+req.body.campTel+','+req.body.campCel+','+ req.body.campNote+','+req.user.username+')',
function(err) {
if (err) {
console.log(err);
}
console.log(connection);
res.redirect(301, '/home');
}
);
这是我的日志。所以如果你能帮我解决一个问题,我真的会很高兴。提前谢谢。
green-camp-7 { ConnectionError: Connection is closed.
green-camp-7 at ConnectionError (/opt/green-camp/node_modules/mssql/lib/base.js:1428:7)
green-camp-7 at Request._query (/opt/green-camp/node_modules/mssql/lib/base.js:1300:37)
green-camp-7 at Request._query (/opt/green-camp/node_modules/mssql/lib/tedious.js:497:11)
green-camp-7 at Request.query (/opt/green-camp/node_modules/mssql/lib/base.js:1243:12)
green-camp-7 at /opt/green-camp/routes/home.js:21:13
green-camp-7 at Layer.handle [as handle_request] (/opt/green-camp/node_modules/express/lib/router/layer.js:95:5)
green-camp-7 at next (/opt/green-camp/node_modules/express/lib/router/route.js:137:13)
green-camp-7 at isAuthenticated (/opt/green-camp/middleware/isAuthenticated.js:8:16)
green-camp-7 at Layer.handle [as handle_request] (/opt/green-camp/node_modules/express/lib/router/layer.js:95:5)
green-camp-7 at next (/opt/green-camp/node_modules/express/lib/router/route.js:137:13)
green-camp-7 at Route.dispatch (/opt/green-camp/node_modules/express/lib/router/route.js:112:3)
green-camp-7 at Layer.handle [as handle_request] (/opt/green-camp/node_modules/express/lib/router/layer.js:95:5)
green-camp-7 at /opt/green-camp/node_modules/express/lib/router/index.js:281:22
green-camp-7 at Function.process_params (/opt/green-camp/node_modules/express/lib/router/index.js:335:12)
green-camp-7 at next (/opt/green-camp/node_modules/express/lib/router/index.js:275:10)
green-camp-7 at Function.handle (/opt/green-camp/node_modules/express/lib/router/index.js:174:3)
green-camp-7 at router (/opt/green-camp/node_modules/express/lib/router/index.js:47:12)
green-camp-7 at Layer.handle [as handle_request] (/opt/green-camp/node_modules/express/lib/router/layer.js:95:5)
green-camp-7 at trim_prefix (/opt/green-camp/node_modules/express/lib/router/index.js:317:13)
green-camp-7 at /opt/green-camp/node_modules/express/lib/router/index.js:284:7
green-camp-7 at Function.process_params (/opt/green-camp/node_modules/express/lib/router/index.js:335:12)
green-camp-7 at next (/opt/green-camp/node_modules/express/lib/router/index.js:275:10) code: 'ECONNCLOSED', name: 'ConnectionError' }
green-camp-7 Promise {
green-camp-7 <rejected> TypeError: Invalid server: undefined
green-camp-7 at new Connection (/opt/green-camp/node_modules/tedious/lib/connection.js:71:13)
green-camp-7 at base.Promise (/opt/green-camp/node_modules/mssql/lib/tedious.js:212:23)
green-camp-7 at ConnectionPool._poolCreate (/opt/green-camp/node_modules/mssql/lib/tedious.js:177:12)
green-camp-7 at ConnectionPool._connect (/opt/green-camp/node_modules/mssql/lib/base.js:241:10)
green-camp-7 at PromiseLibrary (/opt/green-camp/node_modules/mssql/lib/base.js:217:19)
green-camp-7 at ConnectionPool.connect (/opt/green-camp/node_modules/mssql/lib/base.js:216:12)
green-camp-7 at new connect (/opt/green-camp/node_modules/mssql/lib/base.js:1600:27)
green-camp-7 at Object.<anonymous> (/opt/green-camp/routes/home.js:7:18)
green-camp-7 at Module._compile (module.js:541:32)
green-camp-7 at Object.Module._extensions..js (module.js:550:10)
green-camp-7 at Module.load (module.js:458:32)
green-camp-7 at tryModuleLoad (module.js:417:12)
green-camp-7 at Function.Module._load (module.js:409:3)
green-camp-7 at Function._load (/root/.nvm/versions/node/v6.3.1/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
green-camp-7 at Module.require (module.js:468:17)
green-camp-7 at require (internal/module.js:20:19)
green-camp-7 at Object.<anonymous> (/opt/green-camp/app.js:43:12)
green-camp-7 at Module._compile (module.js:541:32)
green-camp-7 at Object.Module._extensions..js (module.js:550:10)
green-camp-7 at Module.load (module.js:458:32)
green-camp-7 at tryModuleLoad (module.js:417:12)
green-camp-7 at Function.Module._load (module.js:409:3) }
答案 0 :(得分:0)
更改主持人:&#39; myhost&#39; - 到mssql服务器的真实IP地址或使用localhost
host: '127.0.0.1' //for example
但您的其他凭据也是错误的。 它必须是真实的端口和用户名以及密码和mssql server的数据库名称