SQL Server LocalDb ProjectsV13 Connection Failure - sql-server

Using SQL Server 2016 (localdb)\ProjectsV13, I created a database - let's call it MyDatabase. All was fine and dandy until I decided I no longer needed it...
So in SSMS 2016, while connected to (localdb)\ProjectsV13, I right clicked on MyDatabase and clicked "Delete".
Now when I try to establish a connection to (localdb)\ProjectsV13, it fails.
Here's the error message:
Cannot connect to (localdb)\ProjectsV13.
Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'MyDomain\MyUserName'. (Microsoft SQL Server, Error: 4060)
So even though I deleted the MyDatabase database (I know - I should of just run a DROP), SSMS is still trying to connect to MyDatabase which of course fails and then prevents me from connecting to (localdb)\ProjectsV13 at all - which is the real issue.
How can I resolve this so that SSMS doesn't try to connect to the deleted MyDatabase database when connecting to (localdb)\ProjectsV13?

Like Sean suggested, I think your default database is the problem. If you're using visual studio, right-click the server and click on the properties option. You'd see a 'Default Database Location' property. Alter the value.
If you're not using visual studio, then change your default database or check that your connection string does not attach the deleted database when you connect. E.g.
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\StaffLoginSystem.mdf;Integrated Security=True;Connect Timeout=30"
Check the value of 'AttachDbFilename' property and edit accordingly.

Related

Login failed for user (but not really) in SSDT in SQL Server 2017

I have a weird problem with SSDT(Visual Studio 2017 server explorer) when I try to view the tables in my Database
In SSMS (SQL Server Management Studio 2017) I created a database with a new user called admin. This user has public and sysadmin permissions and can execute CRUD queries and see the tables in the Object Explorer.
So everything is working fine... However, when I try to connect to the database from VS2017 with SSDT I succeed to connect to the server, but I cant view the tables inside the Server Explorer and it throws me the "Login Failed for user..." error. However when I try the same with the SQL Server Object Explorer it works fine:
Why do i get this error? The credentials are correct otherwise it wouldn't have worked in SSMS and object explorer. My permissions are correct otherwise I wouldn't have been able to create the table and insert data into it. Why do I get the Login failed error in the Server Explorer?
Okay so I managed to fix the "problem",
I was entering the correct credentials, but it was still giving me this error.
So what i tried this time is to eneabled the Save my password checkbox.... and it worked...
I deleted the connection and created a new connection (without checking the Save my password) and again the same error. So i enabled the checkbox again and it worked...
I think when trying to reach for the tables it re-authenticates at the SQL server, and since the checkbox Save my password was not enabled, it didnt have a password to authenticate with.
check your credentials in the properties of the connection

allow to auto generate DBs in sql server under windows authentication

In my web.config I have defined following connection string for a Database that not existing in sqlexpress (code first entity framework approach)
<connectionStrings>
<add name="cityconnectionstring" connectionString="Data Source=.\sqlexpress;Initial Catalog=CityPolulation;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
So once I define this on web.config file I can see cityconnectionstring listed under Server Explorer window Data Conncetion in Visual Studio.
But When I trying to Right Click on above connection string and hit Refresh I'm getting following error in Visual Studio.
Server Explorer Error
server explorer unable to perform this operation see below for
details.
Cannot open database "CityPolulation" requested by the login. The
login failed. Login failed for user 'PC-name\username'.
All the answers related to above error are for existing DBs.
How to set permission that can connect to sqlserver for not existing DBs under Windows Authentication.
What are the Settings I should look in IIS or SQL server Management Studio
When the connection is verified it checks that you have access to the database. If the database does not exist then you cannot verify the connection.
If as your question's title suggests, you want to create the database, then you will still need to connect to a database first. IF you have permissions to create a database then you will certainly have access to the "master" database.
However, I hope that you are not expecting all users to be able to create a database as that would mean that all of your users have far too many permissions to the server!
EDIT Change the connection's database to "master". That will prove that you can connect.
Then manually (in SSMS or whichever tool you use) Create the new database on the server.
Now modify your connection to use the new database connection. You should now be able to connect.
Entity Framework is not capable of creating databases. However, once you have created the database it can create tables and data.

create database permission denied in database ‘master'-sql 2008 R2

So I have installed SQL Server 2008 R2 on windows 7 machine.I was able to login using my windows authentication.Now when I try to create a new database it gives me this error saying "CREATE DATABASE permission denied in database ‘master'".I have tried so many links related to this issue but still it is failing.These are the options that I tried:
1) Went to SQL Server Configuration Manager and then changed the parameters in the advanced tab by adding -m.It still did not help me.When I add this parameter and try to login again using windows authentication it gives me a message saying "Only one administrator can login as it is in single user mode".
2) While Installation I am sure that I added the current user under sysadmin role. I also made sure that the "Mixed mode authentication" is selected and then gave a password for the user "sa".Now when I try to use that password it says "Failed login for user sa".
3) When I login with windows authentication and then check the login mode by following this:
right click on main instance name-->Properties-->Security,it shows that only windows authentication mode is selected.But I am very much sure that I selected the "Mixed mode authentication".Not sure why it is showing it in this way.
4) I also tried to modify the password for sa but the current user has no permissions.I am not able to perform any operation with this user.
I know there are different links for this issue.But I tried all the options that were mentioned in those links but still it is giving me a problem.I have been trying to resolve this issue from a very long time.Could anyone suggest what I am missing here.
Solved! I had the same problem. I figured it out based on info from the article and the comments from the first commentor in this link.
I'll copy the author's steps with the necessary modifications in bold:
Open the Configuration Manager tool from the "SQL Server 2005| Configuration" menu
Stop the SQL Server Instance you need to recover (both SQL Server AND SQL Server Agent. Once in single user mode, if not stoped, the agent will tie up the connection, preventing you from using it with sqlcmd.exe).
In SQL Configuration Manager, click on SQL Server Services on the left, then right click on SQL Server on the right, navigate to the “Advanced” tab (or "Startup Parameters for more recent versions of SQL), and in the Properties text box add “–m” to the end of the list in the “Startup parameters” option.
Click the “OK” button and restart the SQL Server Instance (just SQL Server. Do not restart the SQL Server Agent just yet.) NOTE: make sure there is no space between “;” and “-m”, the registry
parameter parser is sensitive to such typos. You should see an entry
in the SQL Server ERRORLOG file that says “SQL Server started in
single-user mode.”
After the SQL Server Instance starts in single-user mode, the Windows Administrator account is able to connect to SQL Server using
the sqlcmd utility (it is probably here, or someplace very similar: C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE) using Windows authentication. You can use
Transact-SQL commands such as "sp_addsrvrolemember" to add an existing
login (or a newly created one) to the sysadmin server role. The
following example adds the account "Buck" in the "CONTOSO" domain to
the SQL Server "sysadmin" role: EXEC sp_addsrvrolemember
'CONTOSO\Buck', 'sysadmin'; GO
Restart/Start SQL Server Agent, back in the SQL Server Configuration Manager.

Error while trying to connect to SQL Server - localhost

I have checked the instance name, auto close is set to true, allow remote connections on the server is checked. The server is running when I open the SQL Server configuration manager. I have even rebooted. I have created this db the same way as all others. I use Entity Framework and have checked the names in the web.config and they match. This is the default connection string from the wizard - I use for testing before I deploy. I just can't think of anything else to check to figure out why it won't connect. Working inside SQL Server everything is fine.
Here is the general error.
A network-related or instance-specific error occurred while establishing a connection to SQL >Server. The server was not found or was not accessible. Verify that the instance name is >correct and that SQL Server is configured to allow remote connections. (provider: Shared >Memory Provider, error: 40 - Could not open a connection to SQL Server)
Config:
connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQL_1;attachdbfilename="C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQL_1\MSSQL\DATA\A_db.mdf";integrated security=True;connect timeout=10;user instance=True;multipleactiveresultsets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
Thanks in advance.
Let me preface this answer by saying that proper setup of a SQL instance is not as easy as Microsoft would like you to think with the entity framework. It's a little bit involved and requires that you put your DBA hat on for a little bit.
The error you have indicates that the web instance is attempting to connect to the SQL server using Windows Integrated Security. This will work fine if (a) the windows user that the process is running as (which can be configured in IIS) is authorized to log on to the SQL server and has a valid login in the database and (b) if the SQL server is on the same machine or in the same domain as the IIS server.
In light of this, I recommend using SQL Server authentication. If you need to know how to do this, I recommend searching for "SQL Server Authentication setup" - here is an article that I found which might help you set this up.
http://msdn.microsoft.com/en-us/library/aa337562.aspx
In general, I recommend taking the following actions:
Connect to the sql server using MS SQL Management Studio.
Permanently attach your database, then use the Initial Catalog property on your connection string rather than AttachDbFileName
Then set up your login username and password on the SQL server, and create a login in the database for it.
Make sure your login can only execute the stored procedures you want it to execute. Deny it access to running sql statements.
You will also need to add the username and password to your connection string, and set IntegratedSecurity=false.
Let us know how things go once you get your SQL server set up properly.
This error means that your provider code cannot find the SQL Server. If you have checked the server instance name (it should be <yourLocalServer>\SQL_1), then it could be the attachdbfilename= parameter, as this is a really unreliable way to specify the database to connect to (you should be using the Database Name, not the file name), because there are about a hundred reasons that the file name could change that have nothing to do with your application.

How can I change from SQL Server Windows mode to mixed mode (SQL Server 2008)?

I have installed SQL Server 2008 Express Edition, but by mistake I kept the Windows authentication mode.
Now I want to change that to SQL Server mixed mode. How can I do this?
You can do it with SQL Management Studio -
Server Properties - Security - [Server Authentication section] you check Sql
Server and Windows authentication mode
Here is the msdn source - http://msdn.microsoft.com/en-us/library/ms188670.aspx
I had no success with other attempts on a SQL Server 2012. What I did was use SQL Server Management Studio to generate a script to change the value, and got this:
USE [master]
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2
GO
After that, I enabled the sa account using this:
ALTER LOGIN sa ENABLE ;
GO
ALTER LOGIN sa WITH PASSWORD = '<strongPasswordHere>' ;
GO
...then, I restarted the service, and everything worked!
From this:
http://weblogs.sqlteam.com/peterl/archive/2008/06/19/How-to-change-authentication-mode-in-SQL-Server.aspx
One can catch that you may change it through windows registry key
(SQLEXPRESS instance):
"Software\Microsoft\Microsoft SQL Server\SQLEXPRESS\LoginMode" = 2
... and restart service
Open up SQL Server Management Studio and connect to your database server.
Right Click The Database Server and click Properties.
Set the Server Authentication to SQL Server and Windows Authentication Mode.
From MSDN:
To change security authentication mode:
In SQL Server Management Studio Object Explorer, right-click the
server, and then click Properties.
On the Security page, under Server authentication, select the new
server authentication mode, and then click OK.
In the SQL Server Management Studio dialog box, click OK to
acknowledge the requirement to restart SQL Server.
In Object Explorer, right-click your server, and then click Restart.
If SQL Server Agent is running, it must also be restarted.
To enable the SA login:
In Object Explorer, expand Security, expand Logins, right-click SA,
and then click Properties.
On the General page, you might have to create and confirm a password
for the login.
On the Status page, in the Login section, click Enabled, and then
click OK.
Open the registry and search for key LoginMode under:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server
Update the LoginMode value as 2.
If the problem is that you don't have access to SQL Server and now you are using mixed mode to enable sa or grant an account admin privileges, then it is far easier just to uninstall SQL Server and reinstall.

Resources