我需要在Javascript中插入一个字符串到MSSQL表中。如何在Node.JS中为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
摘要是怎么回事的:
- 我定义一个消息字符串。 (请注意,它包含一个单引号)
- 我送该字符串Node.js的服务器
- 我使用Node.js的发送字符串到MSSQL
问题: 在了解了更多关于面向对象的程序之后,我意识到我执行插入的方式受到高度的关注,因为它对SQL注入是开放的。而且,由于字符串中的单引号,代码将在执行SQL查询期间中断。
解决方案: 根据我发现的大多数资源,解决方案是使用“准备好的语句”。
我的问题:
如何在地球上我转换我已经做了,利用准备好的发言?我已经搜索网络HOURS,我找不到一个很好的例子,说明如何使用Node.JS为MSSQL(而不是MySQL)准备好的语句,这对初学者来说是易于理解的。Node.Js