使用Next.js连接SQL Server

wz3gfoph  于 2022-12-10  发布在  SQL Server
关注(0)|答案(1)|浏览(285)

I am having a problem (ConnectionError) while trying to connect Next.js with SQL Server with mssql , I have enabled TCP/IP, and SQL Server Browser is running.
this is my code:

// db.js

import sql from 'mssql'

// connection configs
const config = {
    user: 'test',
    password: '1000',
    server: '.\sqlexpress',
    database: 'DATABASE_NAME',
    port: 1433,
    options: {
        instancename: 'SQLEXPRESS',
        trustedconnection: true,
        trustServerCertificate: true
    },
}

export default async function ExcuteQuery(query, options) {
    try {
        let pool = await sql.connect(config);
        let products = await pool.request().query(query);
        return products.recordsets;
    }
    catch (error) {
        console.log(error);
    }
}
// api/hello.js
import ExcuteQuery from '../../utils/db';

export default async function handler(req, res) {

  console.log(await ExcuteQuery('select * from tbl_category'));

  res.status(200).json({})
}

this is the error:

ConnectionError: getaddrinfo ENOTFOUND .
    at E:\0 - WEB\pos\node_modules\mssql\lib\tedious\connection-pool.js:70:17
    at Connection.onConnect (E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1012:9)
    at Object.onceWrapper (node:events:628:26)
    at Connection.emit (node:events:513:28)
    at Connection.emit (E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1040:18)
    at E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1081:16       
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  code: 'EINSTLOOKUP',
  originalError: ConnectionError: getaddrinfo ENOTFOUND .
      at E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1081:32     
      at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
    code: 'EINSTLOOKUP',
    isTransient: undefined
  }
}

also these are the settings changed:

bybem2ql

bybem2ql1#

我尝试将服务器设置为MY_DESKTOP_NAME\SQLEXPRESS,但它不起作用,但在禁用并重新启用所有设置后,它现在起作用了,这有点像系统中的故障

相关问题