Home > Uncategorized > Connect to #Microsoft #SQL server from #Node

Connect to #Microsoft #SQL server from #Node

Screen Shot 2018-09-01 at 19.44.32

Yes, it’s possible to connect to Microsoft SQL Server from Node JS, even on a Mac OSX environment.

First, install msssql via NPM as follows

npm install mssql

Then, you can write some node code as follows;

const sql = require(‘mssql’)

const config = {
user: ‘xxxxx’,
password: ‘xxxxx’,
server: ‘yoursqlserver.com’,
database: ‘master’,
options: {
encrypt: true
}
}
sql.connect(config, err => {
console.log(err);
new sql.Request().query(‘select @@version’, (err, result) => {
console.dir(result)
});
});

sql.on(‘error’, err => {
console.log(err);
});

Which, in my case gives the output;

{ recordsets: [ [ [Object] ] ],

  recordset:

   [ { :

        ‘Microsoft SQL Server 2016 (RTM-GDR) (KB4019088) – 13.0.1742.0 (X64) \n\tJul  5 2017 23:41:17 \n\tCopyright (c) Microsoft Corporation\n\tEnterprise Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: )\n’ } ],

  output: {},

  rowsAffected: [ 1 ] }

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: