如何使用Azure函数将HTTP查询参数插入到Azure SQL数据库中

时间:2018-07-09 16:39:49

标签: javascript azure-sql-database azure-functions

我有一个具有三个查询参数的azure函数,我想将它们插入到Azure SQL数据库中。下面是我编写的代码,但是,我收到一条错误消息,指出无效列。选择查询工作正常。

const Connection = require('tedious').Connection;
const Request = require('tedious').Request;

module.exports = function (context, req) {
 context.log('JavaScript HTTP trigger function');
 let bId = req.query.binId
 let bIP = req.query.IPAddress
 let btype =req.query.bincat

 context.log(bId);
 context.log(bIP);
 context.log(btype);

 const config = {
    userName: 'used the necessary input',
    password: 'used the necessary input',
    server: 'used the necessary input',
    options: {
        database: 'used the necessary input',
        encrypt: true
    }
};
if (bId && btype && bIP) {
    context.res = {
        body: "Details: " + (bId) + ","+(bIP)+","+(btype) 
    };
}
else {
    context.res = {
        status: 400,
        body: "Please pass a name on the query string"
    };
}


context.log('config done');
const connection = new Connection(config);
connection.on('connect', err => {
    err ? context.log(err) : executeStatement();
});
context.log('connection done');
const query = 'insert into Bins(BinId, IPAddress, BinType) values (bId,bIP, btype)';
//const query = 'select * from Bins';
const executeStatement = () => {
    const request = new Request(query, (err, rowCount) => {
        context.log('inside exe');
        err ? context.log(err) : context.log(rowCount);
    });
    request.on('row', columns => {
        columns.forEach(column => context.log(column.value));
    context.done();
    });
connection.execSql(request);
};
 context.log('executed statement');
 //context.done();
// context.done();
};

错误是在插入命令中。

1 个答案:

答案 0 :(得分:0)

您缺少参数request.addParameter(...,参数也应以@开头。有关更多详细信息,请参见文档https://tediousjs.github.io/tedious/parameters.html

创建请求对象后,您需要做类似

的操作
var TYPES = require('tedious').TYPES

const query = 'insert into Bins(BinId, IPAddress, BinType) values (@bId,@bIP, @btype)';
const request = new Request(query, .....

request.addParameter('bId', TYPES.Int, bId);
request.addParameter('bIP', TYPES.VarChar, bIP);
request.addParameter('btype', TYPES.VarChar, btype);


...