when to specify providerName="System.Data.SqlClient" in a connection string - connection-string

I had an issue with a Windows service running under NT AUTHORITY\LOCAL SYSTEM whereby the service could not connect to a database.
Login failed for user 'NT AUTHORITY\LOCAL SERVICE'. Reason: Failed to open the explicitly specified database.
The only difference between the connection string specified for this particular service and another service running under the same account was that the providerName attribute was missing. Once I added
providerName="System.Data.SqlClient"
to the connection string the service could access the database. Can anyone explain why this would be? I thought System.Data.SqlClient was the default provider.

When you want to use the SQLClient provider to manager the connection.
SQLClient provider manages connections to Microsoft SQL Servers.
Here is a code example
You never have to specify it though, as its also the default value for the provider.

Related

Not able to login via specified user in localhost SQL Server connection string

I am new to SQL Server and I am trying to connect a localhost SQL Server. And I am having the following exception:
Cannot open database "MyDatabase" requested by the login. The Login failed Login failed for user "MyCodingPC\Cyborg".
And also I don't understand why it is getting logged in via that username when I have specified another user in the connection string.
My connection string
Data Source=xxx.xxx.xx.xxx,1433\(localdb)\MSSQLLocalDB;Network Library=DBMSSOCN;Initial Catalog=MyDatabase;User Id=MyNewUser;Password=pass##$word;Integrated Security=True
LocalDB cannot be used over TCP. So to accomplish my requirements I used SQLExpress instead.
LocalDB is supposed to work only for local access; remote access is not possible here.

Docker .NET Core API connection to SQL Server through Kerberos

Someone have success to connect a Dockerized .NET Core API 2.2 with SQL Server located in external client cloud server through Kerberos?
Here we're facing these issues:
Scenario 1:
If we use a connection string like this:
Server=tcp:SERVER_IP_ADDRESS,1433; Database=DB_NAME; User Id=USER; Password=PASSWORD;
then, it takes a long time and throws the exception like this:
SqlException: A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 0 - Success)
Scenario 2:
If we use a connection string like this:
Server=tcp:SERVER_IP_ADDRESS,1433; Database=DB_NAME; User Id=USER; Password=PASSWORD; Trusted_Connection=True;
then, the exception is:
SqlException: Cannot authenticate using Kerberos.
Ensure Kerberos has been initialized on the client with 'kinit' and a Service Principal Name has been registered for the SQL Server to allow Kerberos authentication.
ErrorCode=InternalError, Exception=Interop+NetSecurityNative+GssApiException: GSSAPI operation failed with error - Unspecified GSS failure.Minor code may provide more information (SPNEGO cannot find mechanisms to negotiate).
So, our hands are tied and we don't know where to run.
Can u help us?
Thanks in advance.
If you don't need strictly kerberos to authenthicate, just use sql user nad password.
To do that create a user on sql server only (not in windows, use ssms to do it or sql script) and use that user, not the windows one.
It seems that you don't use sql server authentication, at least you don't use sql server user but a windows one and sql server tries to authenthicate that user in AD instead authenticating it locally on sql server.
However if you want to use Windows auth, you probably would need to use windows containers and gMSA accounts, see https://learn.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/gmsa-run-container

How to fix "Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net)"

I have a web application in visual studio 2017 .NET framework 4.7.1 with identity, that connects to azure sql server database. I am getting an error after submitting login form. The exception message is:
"Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net). Some libraries do not send the server name, in which case the server name must be included as part of the user name (username#servername). In addition, if both formats are used, the server names must match."
The error is only in the web server (azure app service). When I run it locally (localhost) it works fine.
I did what error message suggests but, it did´nt work. I searched about this but, none of the suggestions or solutions I found have worked.
If I remove login stuff, application is able to connect and works so, the problem seems to be the connection string I use to connect to identity tables.
I have tried this connection strings in web.config but none of them have worked:
1.
add name="DefaultConnection" connectionString="Server=tcp:myServer.database.windows.net,1433;Initial Catalog=myDatabase;Persist Security Info=False;User ID=myUser#myServer;Password=myPassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False" providerName="System.Data.SqlClient"
2.
add name="DefaultConnection" connectionString="Data Source=myServer.database.windows.net;Initial Catalog=myDatabase;Integrated Security=False;User Id=myUser;Password=myPassword;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
First, did you set the Allow access to Azrue Service ON?
Please check your Azrue SQL Database firewall settings on portal:
Second, get the correct connection string:
Others, please see: Troubleshooting connectivity issues with Microsoft Azure SQL Database.
Hope this can helps.

Problem accessing database for Entity Framework

I've created a windows forms application that would use a client server model for accessing database. I created a connection for Entity Framework Model and it works fine with the following connection string:
connectionString="metadata=res://*/CartonOrderModel.csdl|
res://*/CartonOrderModel.ssdl|
res://*/CartonOrderModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=.\SQLEXPRESS;
AttachDbFilename=E:\Databases\RollMeasurements.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
I wonder if this connection string would still work when i deploy it on client environment which has a central db server and more than one instance of my app would be accessing it from client machines.
I tried changing the connection string to this:
connectionString="metadata=res://*/CartonOrderModel.csdl|
res://*/CartonOrderModel.ssdl|
res://*/CartonOrderModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=.\SQLEXPRESS;
Initial Catalog=RollMeasurements;
User ID=sa;
Password=sapassword;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
But now when I run my app it gives me this exception:
Failed to generate a user instance of SQL Server. Only an integrated
connection can generate a user instance. The connection will be
closed.
Please help me with this. Thanks!
I think you have to remove User Instance=True (or set it to False explicitely). User Instances are local copies of a SQL Express database and not suited for work in a multiuser environment with a central DB.
I think if you want to access to a central server, you need to change the Data Source=.\SQLEXPRESS; line to a central sql server instance.
english is not my mother language so maybe dont understand correctly the question.
Cheers

windows service sql connection problems

I need your help !!!!
I want to connect to sql server from a windows service, but it throw following exception :
Login failed for user 'NT
AUTHORITY\ANONYMOUS LOGON'.
My connection string is declared as follow:
<add name="CoreConnectionString"
connectionString="Data Source=10.10.2.102;Initial Catalog=DataBaseName;
Integrated Security=True"
providerName="System.Data.SqlClient" />
When I use user name and password instead of Integrated Security=True It works but in final deployment I cannot use user name and password.
What is wrong what can I do????
When you define Integrated Security=True in your connection string, whatever user is currently logged in will try to connect to your database. When running this as a console or Winforms app, this is your own user account.
However, if you run it as a Windows NT Service, it's the service account that this service is running under - in your case obviuosly NT AUTHORITY\ANONYMOUS LOGON.
And the error says it clearly: this user account does not have the permission to connect to the SQL Server.
You have several options:
stop your NT service and change the service account to be someone who does have access to the SQL Server
allow the NT AUTHORITY\ANONYMOUS LOGON to log into your SQL Server and use your database
create a specific user (e.g. an "application user") in your SQL Server and change your connection string to use that user specifically:
connectionString="Data Source=10.10.2.102;Initial Catalog=DataBaseName;
user id=Your-Application-User-here;password=The-Secret-Password"

Resources