Local SQL Server database is not connected - sql-server

I am using Entity Framework version 6.0. I want to connect to a local SQL Server database. I am using the following connection string. But I cannot connect to the database. Please give me the solution.
<add name="dbecommerceEntities"
connectionString="metadata=res://*/Models.dbEcommerceMode.csdl|res://*/Models.dbEcommerceMode.ssdl|res://*/Models.dbEcommerceMode.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;Integrated Security=False;Initial Catalog=dbecommerce;User ID=qaws;Password=1q2w!A#S;Connect Timeout=30000;Encrypt=False;Packet Size=4096;App=EntityFramework"" />

Check the usual suspects:
data source=localhost : this means, there must be a default, unnamed SQL Server instance on your local computer - is there? Or did you install SQL Server Express in default mode? Then you'd have to use data source=(local)\SQLExpress instead
User ID=qaws;Password=1q2w!A#S : do you have this user ID and this password as a login on your SQL Server instance?
Initial Catalog=dbecommerce;: does that database exist on that SQL Server instance? Is the login from above defined as a user in this database, and has the permissions needed to do work?

my connection string was correct. one of the file was not uploaded to the server. now it is working correctly.

Here is what I do to test connection to database:
Create a .txt file somewhere in your computer.
Rename the file to .udl
Accept the change extension question and double click the file to open.
In your case, in the first tab, select SQL server and in the second tab, fill in the required fields.
Test the connection and if unsuccessful, it will provide you an error.
If successful, close the dialog and open the .udl file with notepad, you can see the connection string in the second line after ";" (the first semicolon)
HTH

Related

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

Here is the scenario.I'm creating a desktop application with VB.NET (Windows Forms), and using SQL Server 2008 R2 for its database.
I have two computers (PC1 and PC2) and I want to connect PC2 to PC1.
PC 1 has the SQL Server database. What I do is I just copied .exe application in
the debug folder of my vb project and create a notepad for its connection and put them into one folder and transferred it to PC2.
When I run my application to other PC, I get an error
Unhandled exception has occurred in your application. If you click
Continue, the application will ignore this error and attempt to
continue, If you click quit, The application will close immediately.
Cannot open database "DatabaseName" requested by the login. The Login
failed. Login failed for user
My code inside the notepad
Data Source=.;Initial Catalog=DBSAS;Integrated Security=True
What I have done so far is to enable remote connection of SQL Server 2008 R2.
Can someone please give me an advice about this? I am really confused. Thanks
Hi please find sql server instance name at PC 1 using below command
OSQL -L
replace . to the instance name at you connection string.
Specify Data Source= "sql server instance name " in the connection string.
more detail are posted on below link.
How can I determine installed SQL Server instances and their versions?.
On the second pc create a txt file on desktop. Something like test.txt. Then rename it to test.udl Universal Data Link. Double click the test.udl. From there you can produce a valid connection string to the SQL Server. After you are done open the udl file with notepad to get the connection string. Hope this helps.
Specify Data Source=PC1 in the connection string. The . indicates a local SQL Server instance running on the same machine as the application. The error suggests SQL Server is installed on PC2 but doesn't contain the desired database.
EDIT:
The error
Cannot open database "DatabaseName" requested by the login
indicates authentication was successful to the SQL Server but the database context could not be set. If DatabaseName in the error message is the same one as the Initial Catalog in the connection string, then either the database does not exist or the user does not have permissions to use it. However, if the DatabaseName in the error message differs from the connection string, the connection string you changed is not the same one that is actually being used by the application.

Writing a connection string

I am willing to write a connection string to allow one computer connect to another computer's sql server located in the same work group.
Anyway, searching the Internet only confused me and led me to fail tries.
So, if I am willing to connect to an sql express server to a database called "Analyze", what should my connection string look like?
Create a new text file with the extension UDL in a folder.
Right click on the file and click open with "OLE DB Core Services"
Filll in the appropriate entries and save.
Open the file in notepad - voila, your connection string is in there.
Good luck.
There is a post on MSDN explaining how to connect to SQL Server express over a network.
The main points are; you need to configure SQL Server to listen for requests. This can be done using the SQL Browser. You also need to ensure the firewall will allow requests through.

Connection string for SQL Server Express 2014

On Win 7 system I installed SQL Server 2014 Express. I created instance HM1. I've set it up to use mixed mode. I created a SQL Server user and I am able to login with no issue.
My goal is to connect to a database using this SQL Server user using configuration file. This connection will be used for another application. Note: if I disable user/password usage during login I have no problem but I have to use credentials.
For connection I provided machine_name\SQLSERVEREXPRESS\HM1. I also provided username and password. I also changed the service for SQLEXPRESS and HM1 to run as local account.
When I try to connect using that file I've got the error
Can't connect to database
Again if I disable user/password usage I am able to connect using the same file with no problem.
Could someone help me with it?
Thanks
For connection I provided "machine_name\SQLSERVEREXPRESS\HM1".
You just want this:
machine_name\HM1
That's probably not your only problem, but that's all I can tell you until you try that and then come back and include more detail from the error message.
You need to put the un-encoded password in the config file instead of the base64 encoded one. Alternatively, you can also put Trusted_Connection=True; in the connection string if your database server resides on the same server as the application server. This will use the OS credentials instead of the username/password that you provide.

can't connect to the SQL database

I created a database in SQL Server Management Studio and I called it SalonDatabase. I tried to connect to the database in VS C#. For this I created two connectionString and only one of them work so I would like to know why.
This one works:
string connString = #"Data Source=.\SQLEXPRESS;initial catalog=SalonDatabase;Integrated Security=SSPI";
However, the following does not work even though I know for a fact that the file is located in that directory
string connString = #"Data Source=.\SQLEXPRESS;
Initial Catalog=C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF;Integrated Security=SSPI";
The error I'm getting is :
Cannot open database "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF" requested by the login. The login failed. Login failed for user 'USER-PC\USER'
Now I logged in as administrator in my computer. I also read about the user instances in SQL server but I don't see that is the reason why the second connection fails.
The database is created using SQL Server Management Studio so do not use AttachDBFileName. You may use Server Explorer (from View Menu) tool of Visual Studio to obtain the connection string.
Add connection From Server-Explorer + Right mouse click on Data Connections + Add Connection + Select Microsoft Sql Server Data Source (change).
After adding connection, right mouse click to select/open properties windows.
Data Source=.\sqlexpress;Initial Catalog=SalonDatabase;Integrated Security=True
Modify your connection to 'attach' your mdf. Given that your .mdf path has spaces, be sure to enclose it in single quotes.
Server=.\SQLExpress;
AttachDbFilename='C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF';
Database=dbname;
Integrated Security=SSPI;
Trusted_Connection=Yes;
Initial Catalog is used to set the Default Database name when we Connect to a Server.
when you are passing the complete path of the database file the connection String makes a search for a database with name C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF
which actually does not exists therefore it fails.
you can use the initial approach and if you want to use full path then AttachDbFilename can be used as suggested by P.Campbell
By default, when working with SQL Server (any edition), the server part handles all the file-related chores for you. You just create a new database (or attach an existing one) and give it a logical database name and then you refer to that database solely via this database name - you don't care (and don't need to) about the actual files that make up your database.
That's why your first connection string works - you're telling SQL Server (Express) that you want to work with a database called SalonDatabase and SQL Server knows which files this concerns.
Microsoft introduced this notion of being able to "dynamically" create a user-instance of SQL Server Express and attaching a physical .MDF file (instead of referring to a database by logical name) as a developer tool to make things easier - but quite frankly, in my opinion, it's a rather messy concept and has lots of drawbacks.
In this scenario, you basically tell SQL Server in your connection string (using AttachDbFileName=salondatabase.mdf and with User Instance=True) that it should start a separate instance of SQL Server Express for your user context, and automagically attach that physical MDF file specified as its database, and provide you access to that.
As mentioned by another comment, it's a deprecated feature - SQL Server 2012 will bring a new, hopefully slicker concept of local databases. So in my opinion, you should try to avoid using this feature - it's more trouble than anything else.

SQL Server - Database 'Database' does not exist. Make sure that the name is entered correctly

I'm trying to generate my database tables from my Entity Framework model, but I'm getting this error when I execute the generation query:
Database 'Database' does not exist. Make sure that the name is entered correctly.
I'm able to connect to the local server just fine.
My connection string, which was generated by VS when I selected 'Database.mdf':
metadata=res://*/Models.Models.csdl|res://*/Models.Models.ssdl|res://*/Models.Models.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
The database definitely exists, but I've noticed that the only databases it's picking up on the server are called master, model, msdb and tempdb.
Can anyone help?
Edit: I've just realised that the SQL connection dialog that comes up when you click "Execute SQL" allows you to connect to a server. But my DB isn't on a server, it's just a user instance database. But that's the only option, so how would I execute against my database? I have the database open in the 'Server Explorer'.
The connection string is referring to a user instance database is that what you intended? I got the impression possibly not from the question.
If not try changing it to Data Source=ServerName\InstanceName;Initial Catalog=Database;Integrated Security=True;
If you are using a user instance database, specify the full path to the location of the database with USE directive.
For example:
USE [C:\Project\Database.mdf]
GO
When connecting the server, you need to select another data source, then enter the name of the local server on which the database is located.
I can't speak for Entity Framework, but within SQL, "database" is a (very) reserved word. Try calling your database (and it's files) something else, even if just "MyDatabase".
I faced such problem when running a script in SQL Server Management Studio to do some stuff on my Local SQL Server. I noticed that the Query Window that I was running the script in was connected to a Remote SQL Server not my local machine.

Resources