0

我正在尝试使用以下命令将数据插入 SQL Server 数据库:-

CREATE TABLE table_simupush
(
    id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    priority int NOT NULL
)

我可以在本地测试时通过以下代码插入数据:-

function SendSimupushData(data)
{
    SQL.open(connectionString, function(err, conn) {
        if(err) {
            // error handler code
            console.log("Azure database connection opening erro: "+err);
            RESPONSE.send(200, { ResponseMessage: 'error' });
        }
        else
        {
                            // it works locally 
            //var command ="INSERT INTO [dbo].[table_simupush] VALUES ('"+data+"')";
            // it also works locally
                              var command ="insert into [dbo].[table_simupush] values ("+data+")";
            console.log("connectionString open ok");
            InsertInDatabase(command, conn);
        }
    });
}

function InsertInDatabase(command, conn)
{
    SQL.query(connectionString, command, function(err, recordset) {
        if(err){
            console.log("erro: "+err);
            conn.close();
            RESPONSE.send(200, { ResponseMessage: 'error' });
            return;
        }
        else
        {
            console.log("InsertInDatabase successlly inserted");
            conn.close();
            RESPONSE.send(200, { ResponseMessage: 'ok' });
            return;
        }
   });
} 

从 Azure 移动服务 API 尝试相同的代码如下我得到了错误:

// service bus
process.env.AZURE_SERVICEBUS_NAMESPACE= "my_namespce";
// service bus fifthelement access key
process.env.AZURE_SERVICEBUS_ACCESS_KEY= "xxxxxxxxxxxxxxxxxxxxxxx";
// azure sql database module
var SQL = require("msnodesql");
// Solax_db database conneciton string
var connectionString = "Driver={SQL Server Native Client 10.0};Server=tcp:kqgh8bqx1z.database.windows.net,1433;Database=solax_db;Uid=masum@kqgh8bqx1z;Pwd={xxxxxx};Encrypt=yes;Connection Timeout=30;";
var REQUEST;
var RESPONSE;

exports.post = function (req, res)
{
    REQUEST = req;
    RESPONSE = res;
    var data = req.RequestMessage;
    SendSimupushData(data);
}

function SendSimupushData(data)
{
    SQL.open(connectionString, function(err, conn) {
        if(err) {
            // error handler code
            console.log("Azure database connection opening erro: "+err);
            RESPONSE.send(200, { ResponseMessage: 'error' });
        }
        else
        {
            //var command ="INSERT INTO [dbo].[table_simupush] VALUES ('"+data+"')";
            var command ="insert into [dbo].[table_simupush] values ("+data+")";
            console.log("connectionString open ok");
            InsertInDatabase(command, conn);
        }
    });
}

function InsertInDatabase(command, conn)
{
    SQL.query(connectionString, command, function(err, recordset) {
        if(err){
            console.log("erro: "+err);
            conn.close();
            RESPONSE.send(200, { ResponseMessage: 'error' });
            return;
        }
        else
        {
            console.log("InsertInDatabase successlly inserted");
            conn.close();
            RESPONSE.send(200, { ResponseMessage: 'ok' });
            return;
        }
   });
}   

错误信息:

错误:[Microsoft][SQL Server Native Client 10.0][SQL Server]列名“未定义”无效。

您能否让我知道如何将数据从 Azure 正确插入 SQL Server 数据库?

4

1 回答 1

0

您收到该错误是因为data变量中的值未定义。您的查询最终将如下所示:

insert into [dbo].[table_simupush] values (undefined)

由于标识符undefined未知,您会收到没有这样的列名的错误消息。

由于它是一个整数值,您应该解析输入,这也将保护您免受 SQL 注入攻击:

var data = parseInt(req.RequestMessage, 10);

由于该值未定义,因此您当前的数据最终将成为解析错误。我不知道为什么该对象会有一个名为 的属性RequestMessage,因为这不是属性通常的命名方式,但您需要弄清楚您要查找的数据在哪里。

于 2014-03-14T08:21:04.237 回答