azure sql server database connection errors - sql-server

I am pretty new to all the azure thing, till now my project was connected to localhost database.
i managed to create server in azure and uploaded my database to that server, i opened the access for my IP and everything was fine till i tried to use my website with functions that use the database (Sign up).
First, i got an error from the browser says:
Login failed for user '{user}'. so i added the server name to the user name like user#server_name. that solved this error.
After this i tried again and another error came up from the browser Cannot open server "server_name}" requested by the login. The login failed.
I have no idea why the login is failing... when i try to log in to the data base from SSMS i have no problems, i have put the same credentials for login in the connectionString web.config file, so i have no idea what the problem is.
Here is my connectionString:
<connectionStrings>
<add name="myapp"
connectionString="Server=tcp:myapp.database.windows.net,1433;Data Source=myapp.database.windows.net;Initial Catalog=myapp;Persist Security Info=False;User ID={XXXX};Password={XXXXXX};Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Please help.

Try this one instead.
Server=tcp:[serverName].database.windows.net;Database=myDataBase; User
ID=[LoginForDb]#[serverName];Password=myPassword;Trusted_Connection=False;
Encrypt=True;

That solved my problem:
<connectionStrings>
<add name="DB_Name"
connectionString="Server=server_name,1433;Database=DB_Name;User ID=user_name;Password=user_password"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Now everything work locally and from the server too

Related

Cannot open database “Mail” requested by the login. The login failed. Login failed for user

I have created a winform application and now I a want to create a setup file for it.
For this I change my database location and put into my project in folder data, but after I change my connection string it's showing me an error that cannot open mail
Here it's my connection string
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\Data\Mail.mdf;Initial Catalog=Mail;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Please help me I am stuck.
That error would usually occur if there is no file Mail.mdf in the Data folder under the standard data directory for SQL Express.
How are you setting the standard data directory? (Or is it just the default), and are you definitely deploying the file into that folder? Finally, if it really is there, are the permissions on the file and folder appropriate?

How to fix "Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net)"

I have a web application in visual studio 2017 .NET framework 4.7.1 with identity, that connects to azure sql server database. I am getting an error after submitting login form. The exception message is:
"Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net). Some libraries do not send the server name, in which case the server name must be included as part of the user name (username#servername). In addition, if both formats are used, the server names must match."
The error is only in the web server (azure app service). When I run it locally (localhost) it works fine.
I did what error message suggests but, it did´nt work. I searched about this but, none of the suggestions or solutions I found have worked.
If I remove login stuff, application is able to connect and works so, the problem seems to be the connection string I use to connect to identity tables.
I have tried this connection strings in web.config but none of them have worked:
1.
add name="DefaultConnection" connectionString="Server=tcp:myServer.database.windows.net,1433;Initial Catalog=myDatabase;Persist Security Info=False;User ID=myUser#myServer;Password=myPassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False" providerName="System.Data.SqlClient"
2.
add name="DefaultConnection" connectionString="Data Source=myServer.database.windows.net;Initial Catalog=myDatabase;Integrated Security=False;User Id=myUser;Password=myPassword;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
First, did you set the Allow access to Azrue Service ON?
Please check your Azrue SQL Database firewall settings on portal:
Second, get the correct connection string:
Others, please see: Troubleshooting connectivity issues with Microsoft Azure SQL Database.
Hope this can helps.

CREATE DATABASE permission denied in database 'master' when Integrated Security=false - Why try to create a database that already exists?

I have an ASP.net MVC app connecting to (localdb)\v11.0 SQL Server database with a connection string using "Integrated Security" - works fine.
Now I want to connect to the same db with a User ID & PW.
I created the login & user: user100, gave it db_owner permission.
I can open the db with that ID/PW using SSMS.
But when I change my connection string to "User ID=user100 & Password=xxxx", I get the error:
CREATE DATABASE permission denied in database 'master'.\r\nCannot
attach the file
'C:\Projects\ExcellerWeb\ExcellerWeb\App_Data\ExcellerWebCopy.mdf'
as database 'ExcellerWebCopy'.
Why does it try to create a database that already exists?
And the following code returns TRUE:
System.Data.Entity.Database.Exists("UserConnection");
At first I thought it was EF Migrations, but it seems to happen on a simple test project without Migrations.
Here's my connection string:
<add name="UserConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\ExcellerWeb.mdf;Initial Catalog=ExcellerWeb;Integrated Security=False;User ID=user200; Password=****"
providerName="System.Data.SqlClient" />
Remove the Option AttachDbFilename from your connectionstring.
<add name="UserConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ExcellerWeb;Integrated Security=False;User ID=user200; Password=****"
providerName="System.Data.SqlClient" />
This option is there to attach the file when you open the connection.
For more details have a look at this or this MSDN-article
This isn't an answer to the original question, but we got the same error message when we moved a web app and database to new hardware, and a Google search led me here, so hopefully this might help someone...
In our case, the connection string that the web app used was fine, and the database already existed, so the error message was a bit of a surprise.
The problem turned out to be that the service account running the app pool in IIS didn't have the correct permissions on the database - when the account was added to the appropriate group, everything worked fine.

Cannot open database "test" requested by the login. The login failed. Login failed for user 'DT\Test$'

I get the following error when trying to run a web app using windows auth.
Cannot open database "test" requested by the login. The login failed. Login failed for user 'DT\Test$'.
When I run
USE Test
GO
SELECT USER_ID('DT\Test$')
it returns 7
So not sure what the issue is
Here is my connection string
<connectionStrings>
<clear />
<add name="TestModel" connectionString="metadata=res://*/Models.TestModel.csdl|res://*/Models.TestModel.ssdl|res://*/Models.TestModel.msl;provider=System.Data.SqlClient;provider connection string='data source="mydb.com, 1433";initial catalog=Test;Integrated Security=SSPI;multipleactiveresultsets=True;application name=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
I should also add that the webapp works fine when I am logged in as my domain account in visual studio, just not when deployed to the production server
Solved: changed app pool identity to LocalSystem

Deploying code first code migrations based web app to server, but it's not creating the database

I have my own box so I have complete control over the server. I'm using SQL server 2008. I run the application hoping it will create the database, but I keep getting the error:
Cannot open database "blah blah" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\appname'.
I've tried to go into SSMS and add the user IISAPPPOOL\appname but that hasn't helped any. Here's the connection string from my web.config
<add name="Context"
connectionString="Data Source=Server\DbInstance;Initial Catalog=blahblah;Integrated Security=True;User ID=myUser;Password=myPassword"
providerName="System.Data.SqlClient" />
and the initializer from my global.asax
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
Is the app failing because code first is failing and not creating the database or is it because of permissions issues? If I need to provide more specific information I can.
Well, one problem you have: you're defining both Integrated Security=True (i.e. Windows authentication) as well as User ID=...;Password=.... (i.e. SQL Server authentication) in your connection string.
Choose one or the other - but not both at the same time!
If you do have that user myUser specified as a login in your SQL Server, right now, it will not be used, because the Integrated Security=True setting will prevail ...
So change your connection string to:
<add name="Context"
connectionString="Data Source=Server\DbInstance;Initial Catalog=blahblah;User ID=myUser;Password=myPassword"
providerName="System.Data.SqlClient" />
and see if now, you're myUser user will be used.

Resources