node js 使用MSSQL 连接数据库

npm :  https://www.npmjs.com/package/mssql

 

const sql = require("mssql");

const sqlConfig = {
  server: "IP",
  database: "数据库名",
  user: "登录名",
  password: "密码",
  port: 1433,
  options: {
    trustedConnection: true,
  },
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000,
  },
  options: {
    encrypt: false,
  },
};


//连接数据库
(async function () {
  try {
    await sql.connect(sqlConfig);
    const { selectData } = await sql.query`select * from device`; 
    const { insertData } = await sql.query`INSERT INTO device VALUES ('id-valuse111', 'name-valuse', 'status-valuse', 'explain-valuse')`;
    
    console.log("current db:", selectData);
  } catch (err) {
    console.log("ERR:", err);
  }
})();

 

posted @ 2022-03-04 10:19  浅绿色i  阅读(504)  评论(0编辑  收藏  举报