从NodeJS插入Azure SQL数据库

时间:2019-12-03 06:11:07

标签: node.js sql-server azure-sql-database

我正在尝试使用NodeJS界面将数据插入Azure SQL数据库。

sql.connect(config, function (err) {
    if (err) console.log(err);
    // create Request object
    var request = new sql.Request();
    // query to the database and get the records
    var q = "INSERT INTO md.GameMaster(gameCode,gtype,config,gprovider,launchdate,isdeleted) VALUES('"+gcode+"','"+gtype+"','"+config+"','"+gprovider+"','"+launchdate+"','N')"
    console.log(q)
    request.query(q, function (err, recordset) {
        if (err) console.log(err)
    });
});

这是我用于在Azure SQL数据库上运行插入查询的代码。

出现错误

TypeError: The "config.server" property is required and must be of type string.
at new Connection (D:\masterdata\node_modules\tedious\lib\connection.js:89:13)
at shared.Promise (D:\masterdata\node_modules\mssql\lib\tedious\connection-pool.js:61:23)
at new Promise (<anonymous>)
at ConnectionPool._poolCreate (D:\masterdata\node_modules\mssql\lib\tedious\connection-pool.js:12:12)
at ConnectionPool._connect (D:\masterdata\node_modules\mssql\lib\base\connection-pool.js:166:10)
at ConnectionPool.connect (D:\masterdata\node_modules\mssql\lib\base\connection-pool.js:136:12)
at Object.connect (D:\masterdata\node_modules\mssql\lib\global-connection.js:57:29)
at app.post (D:\masterdata\app.js:64:5)
at Layer.handle [as handle_request] (D:\masterdata\node_modules\express\lib\router\layer.js:95:5)
at next (D:\masterdata\node_modules\express\lib\router\route.js:137:13)
INSERT INTO md.GameMaster(gameCode,gtype,config,gprovider,launchdate,isdeleted) VALUES('ATG','Slot','some 5  some 3','Playtech','2019-12-20','N')
{ ConnectionError: Connection is closed.
at Request._query (D:\masterdata\node_modules\mssql\lib\base\request.js:447:37)
at Request._query (D:\masterdata\node_modules\mssql\lib\tedious\request.js:346:11)
at Request.query (D:\masterdata\node_modules\mssql\lib\base\request.js:383:12)
at D:\masterdata\app.js:71:13
at _poolCreate.then.catch.err (D:\masterdata\node_modules\mssql\lib\base\connection-pool.js:241:7)
at process._tickCallback (internal/process/next_tick.js:68:7) code: 'ECONNCLOSED', name: 'ConnectionError' }

我不确定为什么会这样。当我运行选择查询而不是插入查询时。

1 个答案:

答案 0 :(得分:0)

也许您可以尝试使用此node.js代码,我测试了选择查询并插入,一切正常:

const { Connection, Request } = require("tedious");

// Create connection to database
const config = {
  authentication: {
    options: {
      userName: "ServerAdmin", // update me
      password: "****" // update me
    },
    type: "default"
  },
  server: "sqlserver.database.windows.net", // update me
  options: {
    database: "Mydatabase", //update me
    encrypt: true
  }
};

const connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
  if (err) {
    console.error(err.message);
  } else {
    queryDatabase();
  }
});

function queryDatabase() {
  console.log("Reading rows from the Table...");

  // Read all rows from table

    //SQL satement
    var id = 3
    var value ='444444'
    var sql = "insert test3(id,sql) values ("+id+",'"+value+"')"
    console.log('query========='+sql)

  const request = new Request(sql,
    (err, rowCount)=> {
      if (err) {
        console.error(err.message);
      } else {
        console.log('rowCount======='+rowCount);
      }
    }
  );


  connection.execSql(request);
}

参考:Quickstart: Use Node.js to query an Azure SQL database

希望这会有所帮助。