[英]How do I create a prepared statement in Node.JS for MSSQL?
我需要将在Javascript中定义的字符串插入到MSSQL表中。
这是我到目前为止的内容:
Javascript:
var message = "It's a great day today!";
$.post('www.server.com/message='+message, function(response){
console.log(response);
});
Node.js服务器:
//..... a bunch of code has been used to accept the HTTP request and get the message...
// for the purpose of this example, the message is asigned to 'NodeMsg'
var mssqldb = require("../core/mssql");
var NodeMsg = theMessageReceivedFromHTTPRequest;
function saveMessage(message) {
mssqldb.executeMssql("insert into messages (message, status) VALUES('"+message+"', 'new')", function (err) {
if (err) {
httpMsgs.show500(req, resp, err);
}//end if
else {
httpMsgs.sendJson(req, resp, 'success');
}//end else
});
};
mssql.js(node.js文件):
var mssqldb = require("mssql");
var settings = require("../settings");
exports.executeMssql = function(sql, callback) {
var conn = new mssqldb.Connection(settings.mssqlConfig);
conn.connect()
.then(function(){
var req = new mssqldb.Request(conn);
req.query(sql)
.then(function (recordset) {
callback(recordset);
})
.catch(function(err){
console.log(err);
callback(null, err);
});
})
.catch(function(err){
console.log(err);
callback(null, err);
});
};//end executeMssql
正在进行的摘要:
问题:在学习了更多有关面向对象程序的知识之后,我意识到执行插入的方式受到了广泛的欢迎,因为它可以进行SQL注入。 另外,由于字符串中的单引号,所以代码将在SQL查询执行期间中断。
解决方案:根据我发现的大多数资源,解决方案是使用“ Prepared Statements”。
我的问题:
我到底该如何转换已完成的工作以利用准备好的语句? 我在网上搜索了HOURS,但找不到一个很好的示例来说明如何使用Node.JS for MSSQL(不是MySQL)编写准备好的语句,这对于Node.Js的初学者来说是可以理解的。
如果您使用的是Tedious跨平台MSSQL驱动程序实现,则文档位于此处: http ://tediousjs.github.io/tedious/parameters.html
基本上,您为需要注入的值准备一个带有'@xxx'占位符的SQL语句,然后将这些参数的实际值绑定到您的请求,然后执行您的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.