我正在尝试使用以下命令将数据插入 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 数据库?