Node js connection string not accepting backslash (\) in for SQL Server

4bbkushb  于 2023-03-28  发布在  SQL Server
关注(0)|答案(3)|浏览(310)

I have connection string in node js for SQL Server as following:

var webconfig = {
     user: 'sa',
     password: 'aman',
     server: 'Aman\AMAN', 
     database: 'Demo',
     options: {
         encrypt: false // Use this if you're on Windows Azure 
     }
 }

it is showing connection close error.

But if I am using network server like 192.5.5.62 etc then its working fine. I think problem in 'Aman\AMAN', because server name not accepting \ backslash (I think).

The following connection is working for me...

var webconfig = {
         user: 'sa',
         password: 'aman',
         server: '192.5.5.62', 
         database: 'Demo',
         options: {
             encrypt: false // Use this if you're on Windows Azure 
         }
     }

If my guess of \ (backslash) is true, then please suggest me how to fix it with this server name or window authentication etc.

Help will be appreciated.

iqjalb3h

iqjalb3h1#

From your comment, it seems likely that you're using the default driver, which is Tedious. Looking at their docs it seems that they have separated out the hostname and instance name, so you'd want to change your code to be like this:

var webconfig = {
     user: 'sa',
     password: 'aman',
     server: 'Aman', 
     database: 'Demo',
     options: {
         encrypt: false, // Use this if you're on Windows Azure 
         instanceName: 'AMAN'
     }
 }
gajydyqb

gajydyqb2#

First of all I would assume here that you are using tedious module (by looking at the comment of options.encrypt). I wanted to confirm this first with comment but I lack credits to comment. I really wanted to help you because I faced the exact same issue sometime back, so here it goes.

I'll divide your questions in two parts in the context of Nodejs Tedious module.

  1. Support for Named Instances
  2. Support for Windows Authentication

Support for Named Instances

By default tedious uses server and options.port to connect. options.port is the default setting with a default value of 1433. This is the reason configuration mentioned below worked.

var webconfig = {
     user: 'sa',
     password: 'aman',
     server: '192.5.5.62', 
     database: 'Demo',
     options: {
         encrypt: false // Use this if you're on Windows Azure 
     }
 }

But if you want to use named instances in the connection configuration, then you must use options.instanceName as shown below. See the documentation .

var webconfig = {
     user: 'sa',
     password: 'aman',
     server: 'Aman', //FQDN is preferred, so assuming you can ping this
     database: 'Demo',
     options: {
         instanceName: 'AMAN'
         encrypt: false // Use this if you're on Windows Azure 
     }
 }

In your case, however it isn't required to connect using named instance because you are able to connect using hostname\IP itself.

Also, you could either use options.port or options.instanceName but not both in the configuration at a time.

This brings me to your second question.

Support for Windows Authentication

Tedious module do not support Windows Authentication to connect to SQL Server instances currently. See here. However, there are few bright minds who already are working on it. Look at this open PR #497 . Feel free to contribute to the Tedious community.

BONUS TIP: Going forward, instead of guessing the root cause, you could also make use of debug event by tedious module which will route you in the right direction. Make sure to disable it in production.

Please mark this as an answer if it helped you. Cheers!

aydmsdu9

aydmsdu93#

Use four backslashes to get one in output

snowflake.createStatement( { sqlText: `insert into log_table (message) values ('\\\\newdata')` } ).execute();

will result in \newdata

相关问题