SQL Server connection to node js

后端 未结 3 1272
-上瘾入骨i
-上瘾入骨i 2020-12-30 11:18

I am trying to establish a connection between nodejs project and server running Microsoft SQL Server 2005. I am using a node module mssql, but I get these error

相关标签:
3条回答
  • 2020-12-30 11:29

    It work for me. First install mssql by npm (npm install --save mssql)

    var sql = require('mssql');
    

    for local sql server

    var sqlConfig = {
      user: 'sa',
      password: 'admin',
      server: 'CBMOBILESHAMIM\\SQLEXPRESS',  
      database: 'databaseName'
    };
    

    for azure server

    var sqlConfig = {
      user: 'adminLogin',
      password: 'admin',
      server: 'severname.database.windows.net',    // don't add tcp & port number
      database: 'databaseName',
      options: {
        encrypt: true
      }
    };
    

    Now connect to server

    (async function () {
      try {
        console.log("sql connecting......")
        let pool = await sql.connect(sqlConfig)
        let result = await pool.request()
          .query('select * from Subject')  // subject is my database table name
    
        console.log(result )
    
      } catch (err) {
        console.log(err);
      }
    })()
    

    for more details check mssql

    0 讨论(0)
  • 2020-12-30 11:37
    var webconfig = {
    
    user: 'login',
    
    password: 'sa@123',
    
    server: 'localhost', 
    
    database: 'TestDB',
    
    
    
    options: {
    
        encrypt: false // Use this if you're on Windows Azure 
    
    }
    
      }
    
    
    
    
     var express = require('express');
    
     var sql = require('mssql');
    
     var http = require('http');
    
    
    var connection = new sql.Connection(webconfig, function(err) {
    var request = new sql.Request(connection); 
    request.query('select * from Users', function(err, recordset) {
       if(err)      // ... error checks 
            console.log('Database connection error');
    
    console.dir("User Data: "+recordset);
    });
     });
    
     var app = express();
    
     var port = process.env.PORT || 8000;
    

    or visit here : https://nodejsbeginersprograms.blogspot.in/2017/02/nodejs-basic-tutorial-with-mssql.html

    0 讨论(0)
  • 2020-12-30 11:45

    Put your var req = new sql.Request(conn) inside connect.

    // config for your database
    var config = {
        user: 'sa',
        password: 'mypassword',
        server: 'localhost', 
        database: 'SchoolDB' 
    };
    
    // connect to your database
    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
        request.query('select * from Student', function (err, recordset) {
    
            if (err) console.log(err)
    
            // send records as a response
            res.send(recordset);
    
        });
    });
    
    0 讨论(0)
提交回复
热议问题