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 Mobile Service 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