Home > Software design >  Connection string that allows sql authentication
Connection string that allows sql authentication

Time:10-06

I am struggling to connect my C program to MS SQL Server database.

My connection string is like this:

#define CONNECTION_STRING       "DRIVER={SQL Server};SERVER=/* */;DATABASE = /* */;Integrated Security = no; User Id = /* */;Password = /* */;"

But when I try to connect with it the program says:

[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'MY\My_windows_name'.

It always try to connect with windows authentication. But why? According to the official Microsoft documentation concerning Integrated Security parameter:

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

Also I believe that database settings are fine because I can connect to it with PHP with no problems at all. Also when I connect using SSMS and SQL Authentication it will pass me through as well. Seems like the connection string is the problem.

EDIT. Let me paste my attempts I've tried since my initial post.

#define CONNECTION_STRING       "DRIVER={SQL Server};SERVER=/* */;DATABASE = /* */;UID = /* */;PWD= /* */;"
#define CONNECTION_STRING       "DRIVER={SQL Server};SERVER=/* */;DATABASE = /* */;UID  = /* */;PWD = /* */;Trusted_Connection=no;"
#define CONNECTION_STRING       "DRIVER={SQL Server};SERVER=/* */;DATABASE = /* */; User ID = /* */; Password = /* */; Trusted_Connection=no;"

All result with the same... Let me also add that I am trying to run it on Windows Server 2019 Standard

CodePudding user response:

I'm pretty sure these are supposed to be:

UID = /* */;PWD = /* */;"

On checking comments I see I'm not alone.

In addition, you should consider removing

Integrated Security = no;

because no is the default and it might be misspelled and giving you another problem.

CodePudding user response:

I finally managed to make it work.

Here is a connection string that works for me:

#define CONNECTION_STRING       "DRIVER={SQL Server};SERVER=/* */;DATABASE = /* */;Authentication=SqlPassword;UID  = /* */;PWD = /* */;Trusted_Connection=no;Encrypt=no;"

It looks this line below was required to force it to log with SQL authentication.

Authentication=SqlPassword

  • Related