SQL Server Express 2008 connection issue; trying to connect to localhost - c#

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.

Related

Cannot open database "Life" requested by the login. The login failed. Login failed for user

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

login failed for user [MyUserName] , when I run my windows form app

I made a windows form app witch uses sql server as it's database . In my own system, my app is working great with no errors but when I run it in another system it gives me the error - "login failed for user [MyUserName]"
Also I used sql authentication to attach to my database
Here is my connection string
string connectionSt = "Initial Catalog = IndustryContracts; Uid=****;Pwd=****";
Witch you Database use ? MS Sql Server Or LocalDb ?
LocalDb: must change connection string after publish More Info
Sql Server: you mus install MS Sql server each PC you want install your app and Resturn your database
I Suggest you use sa user for Login Sql server in connectionString,
Enable sa user

Cannot open database error

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.

Login Failed for existing user SQL Server 2012

Using C# I'm trying to connect to an existing DB in SQL Server 2012, from Visual Studio 2012.
I used the Visual Studio wizard to get my connection string, and I told it to use Windows Authentication (what I am using for my SQL DB, hosted locally on my system).
This is the error I get:
Cannot open database "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQ\DATA
\StockHolesDB.mdf" requested by the login. The Login failed.
Login failed for user '***\***'.
My connection string is "Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\StockHolesDB.mdf\";Integrated Security=True;Connect Timeout=30"
Looking around online I saw that one of the solutions is to set that user up in the sql server. But that user is already set up in the server. A different username, but the same login name. And when I tried to add a new user with the same login name but different username it wouldn't work.
EDIT: I tried changing the DB name, and that didn't work either.
Make sure you enabled 'SQL Server and Windows Authentication mode' on MSSQL (right click on server, under security tab)

Cannot open database "dbname" requested by the login. The login failed. Login failed for user "machinname\username"

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

Categories