I have the below query in my console appliaction
SqlConnection myConnection = new SqlConnection("user id=abc;" +
"password=xyz;server=test;" +
"Trusted_Connection=yes;" +
"database=tested123; " +
"connection timeout=30");
myConnection.Open();
When I run the above application, it is working fine .But when publish this, it is throwing an error for the other users who are not having access to this DB .To avoid this I have created one service account "abc" and added it to the required DB.But it is still failing with the below error .
System.Data.SqlClient.SqlException (0x80131904): Cannot open database
"tested123" requested by the login. The login failed.
Login failed for user >> user who is running this app.
How to run EXE with some custom account for all the users ?
I think you have to remove this row
Trusted_Connection=yes;
Using Windows credentials and is 100% equivalent to:
Integrated Security=SSPI;
Or
Integrated Security=true;
If you don't want to use integrated security / trusted connection you will need to specify user id and password
More about .NET Data Provider for SQL here
More about connection string here
As you can see in MSDN
Integrated Security -or- Trusted_Connection
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
This is because you've got Trusted Connection in your connection string.
That means you're effectively using Windows Auth, not the details in the string. Remove that and you should be ok.
By using Windows authentication you're asking the application to make the connection as the user who is running it.
You could go to the web http://www.connectionstrings.com/ and find out the connection string of your specific data base.
On the other hand, there is another similar question about this here:
SqlException: System.Data.SqlClient.SqlException (0x80131904)
Replace
Trusted_Connection=yes;
to
Integrated Security=false;
in your connection string.
Related
I am getting the following in visual studio:
Cannot open database "Life" requested by the login. The login failed. Login failed for user...
My sqlconnection string is as follows:
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Life;Integrated Security=True");
What can I do to get this error to be solved.
In SQL Server Management Studio under Security-Studio-Properties under user- User mapping I tick db_owner
SQL Server agent is running
The database is on a local PC and not on a network
There are a number of things that you can check. https://learn.microsoft.com/en-us/troubleshoot/sql/connect/resolve-connectivity-errors-overview
The most likely problems can be solved by making sure you have the correct database name and user name. Your user name will be MYPCNAME\MyLoginName in order to use integrated security
ASP.NET 4.51, WebForms, VS2013
I am using Quartz.NET to do some background processing where ultimately I make a connection to my SQL Server. This all works locally on my development machine against IIS Express, but when I deploy it to my staging server running IIS I run into problems.
The code to connect to the database could not be simpler:
myConnection = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=MyDB;User ID=sa;Password=myPass");
however this throws an exception of:
System.Data.SqlClient.SqlException: Login failed for user 'IIS
APPPOOL\somehost.somedomain.com'.
I am 100% confident the connection string is correct as when used in a normal page it works just fine. So what is throwing me is the reference to IIS APPPOOL.
Is the SqlConnection somehow not using the connection string it was passed? Doing some form of weird user impersonation when the connection is being made?
Put another way. How do I make the SqlConnection() work from within the thread when I know the connection string is correct?
try Application Pool --> Advanced Settings
NetworkServices
Looks like it's failing trying to open a connection to SQL Server.
You need to add a login to SQL Server for IIS APPPOOL\ASP.NET v4.0 and grant permissions to the database.
In SSMS, under the server, expand Security, then right click Logins and select "New Login...".
In the New Login dialog, enter the app pool as the login name and click "OK"
You can then right click the login for the app pool, select Properties and select "User Mapping". Check the appropriate database, and the appropriate roles. I think you could just select db_datareader and db_datawriter, but I think you would still need to grant permissions to execute stored procedures if you do that through EF. You can check the details for the roles here
Refrance : By jeff-ogata
Try setting Integrated Security to false in your connection string. That should prevent it from trying to use Windows authentication.
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx
Since you supplied the user id and password, you need to set the "Integrated Security=False" to the connection string. By doing so, it will not use the Application Pool user to connect to the database. For example:
myConnection = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=MyDB;Integrated Security=False;User ID=sa;Password=myPass");
I am really frustrated, this is very simple but didn't work with me. I'm trying to connect to my SQL Server which I don't need username and password when running SSMS. My server name is HP\SQLEXPRESS and I'm using the following connection string:
string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Students.mdf;Integrated Security=SSPI;";
and I get the following error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Cannot open database "Students.mdf" requested by the login. The login failed.
Login failed for user 'HP\Aymen'.
And when I specify the full address of my database as follows:
string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=c:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLEXPRESS\\MSSQL\\DATA\\Students.mdf;Integrated Security=SSPI;";
it also gives me the same error.
Any suggestion?
when you're connecting from the SSMS, you have admin privileges.
i think this is not the case of the user that run your program.
however, i would suggest you to create a specific user using create login
then, simply just add your parameters to
sql server connection string
try this
I think Your are using Windows Authentication
string connectionString = "Data Source=HP\\SQLEXPRESS;
Initial Catalog=Students;Integrated Security=SSPI;"
Try using sa login to access the database
User ID=sa;
Password="your password without quotes";
and
data source=.
in your connection string
UPDATED CONNECTION STRING
FOR WINDOWS Auth.
string connectionString = "Data Source=.;Initial Catalog=Students;Integrated Security=SSPI;"
FOR SQL AUTH.
string connectionString="Data Source=.;User ID=sa;
Password=; Initial Catalog=Students; " providerName="System.Data.SqlClient";
I believe you have permissions problem for that user.
have you tried login with other user (admin user) to that db?
use your admin user to login to ssms.
under security, change the server Roles and user mapping to that specific user you want, and try to login again. do forget to give that user permission to "master".
seems authentication issue here. you should specify the user id too in the connection string.
string connectionString = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Students;Integrated Security=SSPI;User ID=PCName\WindowsUser;";
If still doesn't work then try this also.
string connectionString = "Server=HP\\SQLEXPRESS;Database=Students;Trusted_Connection=True;
The problem solved. It seems that the problem is because I already have made a connection to the database using Entity Framework model. When I removed it and run the application again everything went fine.
I've become stuck with an issue that I am sure is on- the- nose simple to fix, but this is my first time trying to connect to SQL Server Express 2008 R2. It's on my local machine, and my error is the standard:
"Cannot open database "Market" requested by the login. The login failed. Login failed for user 'KR\user'."
In SSMS, here are some of the connection properties:
Authentication Method: Windows Authentication
User Name: KR\user
Server Name: KR\SQLEXPRESS
Instance Name: SQLEXPRESS
Here is my connection string:
Data Source=localhost\SqlExpress;Initial Catalog=Market;Integrated Security=True
Here is how I'm attempting to connect via C#:
connectionString = "Data Source=localhost\\SqlExpress;Initial Catalog=Market;Integrated Security=True";
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
Edit:My attempt with a different syntax was this connection string:
"Data Source=.\SQLExpress;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Market.mdf';Integrated Security=True;
Using it with the same calling code changes the error to:
An attempt to attach an auto-named database for file C:\Program
Files\Microsoft
SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Market.mdf failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
Edit: I found the properly formatted connection string by right- clicking the database in database explorer, going to properties, then copy/ pasting the connection string property into my code. Thx...
You need to setup the permissions for this user on your instance of SQL Server Express.
A server login (using Windows Authentication), a database user that is attached to the login and the permissions on the databases this user will be accessing all need to be setup.
Looks like you NT security. Have checked that the "KR\user" has been granted rights on the Marketing database. Open the database "Market" > Security > Add User. Grant them DBO rights and try again.
I am trying to run a console application written in C# using visual studio 2010
in which i am accessing a temp database of Microsoft Sql Server Managment Studio.
But the application gives exception as follows:
"Cannot open database "dbname" requested by the login. The login failed. Login failed for user `machinname\username""
And my connection string is as follows:
con.ConnectionString = #"Data Source=.\SQLEXPRESS;Initial Catalog=temp;Integrated Security=True";
Why might this fail with such an error?
The user that the application is running under (probably you since this is a console application), does not have login permissions on the database.
Integrated Security=True indicates that Windows Authentication is being used - either use a user that has appropriate permissions on the database, or grant the appropriate permissions to the user.
If you change the connection string to have a sql server username and password that can be associated with the database then it will work if the application is not running under a windows identity that is not associated with sql server windows authentication
Data Source=.\SQLEXPRESS;Initial Catalog=temp;Persist Security Info=True;User ID=<username>;Password=<pw>"
the application is running under a user that is not recognised in sql server