Sql Exception 0X80131904 Login failed for user ". The user is not associated with a trusted sql server connection - sql

I'm developing a windows application using c# .Net Framework 4.
I have 2 machines; Test and production, connecting to a remote server which has the SQL Server here's my connection string:
<add name="MealConnectionString"
connectionString="Server=MySQLServer;Database=MEALDB;User Id=Meal;Password=Meal1;Trusted_Connection=True;"
providerName="System.Data.SqlClient" />`
When I run the program from the test machine there's no problem, but when I publish the program and run it from the production machine it gives the error mentioned in the title.
When I remove the trusted connection it gives me a different error: Login failed for user "Meal"
do you have any idea what could be the problem ??
Latest Update :
I tried the sa Sqlaccount and removed the trusted connection and it worked , when i try the same for the user meal it doesn't work whereas the MEAL suer has the same privileges as the sa account

I did it I just created a new sql account and gave it a db_owner permission over the database and it worked.

Related

Scaffold-DbContext Error "Login failed for user MicrosoftAccount\my_email#gmail.com"

Trying to execute this command in Visual Studio Package Manager Console:
Scaffold-DbContext "Server=my_server\SQLEXPRESS;Database=FoodStore;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
but I'm getting this error:
Cannot open database "FoodStore" requested by the login. The login
failed. Login failed for user 'MicrosoftAccount\my_email#gmail.com'.
I've tried creating a login in Security for 'MicrosoftAccount\my_email#gmail.com' as db_owner in Microsoft SQL Management Service Studio, as well as signing in with local accounts instead, but I'm still getting an error in Visual Studio when running Scaffold-DbContext.
I was also facing the same error but Ifound the solution. In scaffolding string I am using the server name "LAPTOP-82GAEQ6T\SQLEXPRESS" but in SQL server I am using only "LAPTOP-82GAEQ6T". That's the same error I was facing, please check your server connection name on both sides, on the server side and your connection string. Best of luck.
I encountered the same issue as you have mentioned i.e.
Cannot open database "dbxxxxx" requested by the login. The login failed.
Login failed for user 'xxxxx\xxx'.
And after working around with it, I found that I had to carefully check the value of
server attribute.
"Server=.\SQLEXPRESS;Database=FoodStore;Trusted_Connection=True;" also works well.

SqlException (0x80131904): Login failed for user Mdtappsrv\mdtusr

I have created a local useraccount on the server (Mdtappsrv\mdtusr), from this account able to login to sql server and can access the database MDT. but when i try fetch the same database from IIS, getting
SqlException (0x80131904): Login failed for user Mdtappsrv\mdtusr
webconfig
Since SQL was installed along with the MDT installation, there was no option to enable mixed mode or to set SA password. To fix this problem created a new SQL instance, this can be achieved by installation of SQL express edition again.
During the installation will get option for changing to Mixed mode and setting up the SA password.

Contained User working for website not for console application

So we have a number of databases in SQL Server set to "Partial" containment. We do this as they are part of a AAG, and it means the users can be contained in the database so when it's copied over to the other server the users are copied too.
I have the site using one such user and that works fine. However I want to point a console application at the same database using the same password. I copied the connection string but when I try to run the console application I get both the following errors logged in the event viewer:
Login failed for user 'MyUser'. Reason: Could not find a login matching the name provided. [CLIENT: SERVERIP]
Login failed for user 'MyUser'. Reason: Password did not match that for the user provided. [Database: 'MyDatabase'] [CLIENT: SERVERIP]
If I create a login and a user in the traditional SQL way I can connect fine but then if we ever failover to the other server the login won't exist. If I create a new contained user it fails in the same way. The contained user works fine on a website running on the same server with the same connection string.
Any ideas why the console app can't connect while the site can? It's SQL Server 2012 if that makes any difference and a .net site and console app written in C#

"Cannot open database - login failed" . Works in Management Studio, not code

Oh yes, the famous error:
Cannot open database "MYDBNAME" requested by the login. The login failed. Login failed for user 'USERNAME'.
Let me explain my situation:
I have a database server, which has a SA account and my user account. The user has the correct mappings to my database (dbowner).
I can open the database with this user, using SQL Management Studio 2008 R2, both on the SQL server itself (using localhost), and on my frontend server.
However, when I run my application, I get the login failed error.
Strange thing is I have a test application and a production application on our frontend server. The test application has test databases on the same database server, and there are no login problems here. The only difference between the two, are the prefix which differs from "TEST" to "PROD". The test application works, the production however, doesn't. The user mappings and seem it should work on both.
Any ideas?
EDIT:
Our connectionstring:
<add key="umbracoDbDSN" value="Server=Websqlsrv01;Database=PROD_Databasename;User ID=umbraco_user;Password=password;Trusted_Connection=False" />
Sql server has a dichotomy login/user
they are 2 different entity:
http://www.akadia.com/services/sqlsrv_logins_and_users.html
maybe your sql login is not associated with an user for database PROD_Databasename but only for TEST_Databasename
another cause could be the user default database or eventually different database schemas
you can easily check if one of those is the problem simply creating a new user for PROD_Databasename and using it in connectionstring.
another way is to execute
exec sp_helpuser
in both databases (PROD and TEST) and see if the username "umbraco_user" is listed.
Anyway it is not a good practice to use same login for 2 different users:
https://dba.stackexchange.com/questions/16374/is-there-any-benefit-to-having-one-sql-server-login-for-multiple-users
Sometimes you need to use servername\instancename in your connection string if you've setup something other than a default instance. Use this link to find your instance name. Give that a try!
right click on you application and run as admin. This should work!

SQL Error Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection when I'm trying to open the WebPart

I've created a custom Web Part for SharePoint that interacts with SQL.
Everything worked fine on my DEV server.
After I moved the WebPart to the client's server I started having problems.
I get Error Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection when I'm trying to open the WebPart.
I've searched for solution for a few hours by now and everything I have found doesn't seem to work in my case.
This is how my connection string looks like:
<add name="MyDataEntities" connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
provider=System.Data.SqlClient;provider connection string="Data Source=ServerName;Initial Catalog=DBName;
Trusted_Connection=yes;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
The SharePoint Web App with the web part and SQL DB are on two different machines.
Here's what I've tried:
1). Made sure SQL uses Mixed mode authentication
2). Made sure the account I'm using has rights to access SQL
3). Tried replacing Integrated Security=True; in the connection string with the User ID = UserID; Password=Password; where UserID and Password were the account IIS is running under.
I ran profiler while clicking on the link and it looks like the app is not using the account’s credentials and is trying to log in anonymously.
Any help is appreciated, I'm desperate because this must be up and running by tomorrow.
Thanks in advance!
Try SPSecurity.RunWithElevatedPrivileges: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
This method will run code as the ASP.Net application pool identity. Wrap your database calls with it.