Unable to access to Db after publish of .net API - c#

I write a small ASP.NET web application with a controller that can access to my local db to retrive informations. When I start the application from visual studio everything works fine, but when I publish the application to IIS I get this error:
"An error has occurred.","exceptionMessage":"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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance."
I can't understand why the application works from visual studio but fails after the publish. My connection strings (automatically generated when I create the controller with EntityFramework) are :
<connectionStrings>
<add name="ToDoContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=ToDoContext-20190205170505; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|ToDoContext-20190205170505.mdf"
providerName="System.Data.SqlClient" />
I noticed that when I run the application form VS in the Url there is the port number, meanwhile from IIS there isn't (I don't know if this fact could have any impact). Any suggestions? Thank you.

This connection string is pointing to an instance of LocalDb. It is highly unlikely that you have this installed on the server, hence getting the connection error.
You need to find out what SQL server instances are available to your IIS application. In a staging or QA environment it might just be localhost but in production it is likely to be separate server.
It is also unlikely that Integrated Security will work (and probably shouldn't be made to work). You need to obtain a username and password from the DBA and use that.

Because you are using integrated security, make sure the applicationpoolidentity used by IIS is setup for SQL Server as per here: https://blogs.msdn.microsoft.com/ericparvin/2015/04/14/how-to-add-the-applicationpoolidentity-to-a-sql-server-login/

Related

Stumped trying to connect IIS 7 to my localdb running inside of Visual Studio 2015

For the life of me I am unable to connect to my localdb hosted database created in Visual Studio 2015 to my local IIS 7 instance running on the same machine. IIS Express works fine but when I wanted to test things as a user outside of my network I dabbled with a Local copy of IIS.
I have tried everything from ensuring the app pools have the right privileges, that my connection strings are accurate and just about every thing else I have found on the web but I cannot for the life of me get this working.
The error I am getting is the following: 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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
)
Connection String in VS2015:
Any other details needed to help troubleshoot ask and I will provide.
Thank you in advance for reading.

MultiSubnetFailover=true not working for listeners with multiple ips

My connection string looks as follows:
<add name="CONN_STRING" connectionString="Data Source=Listener1,28589;MultiSubnetFailover=True;Initial Catalog=DataDB;Persist Security Info=True;User ID=xxxxxx;Password=xxxxxx" providerName="System.Data.SqlClient"/>
When I use Listener1 which has 1 active ip, it works fine. But when I use Listener2 which has 2 nodes (1 active, 1 inactive),it doesn't work.
My initial guess was that my application was using old libraries to make the connection. But the application works on a different machine.
Application is targeting .net 4.5.1 framework. And both machines have 4.6.2 installed. Is there anything else that I need to change?
This is the error I get when it doesn't work.
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.
Multi-Subnet-Fail-Over represents the possibility to cross the subnet in the database connection. Windows Server 2008 or later and SQL server 2012 is required because they have a logical OR sequence and use the AlwaysON AG configuration.
Windows 2003 and SQL server 2008 have a logical AND sequence and to be functional they need the clustered resource to remain online.
See doc Microsoft at: https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/listeners-client-connectivity-application-failover?view=sql-server-2017#SupportAgMultiSubnetFailover

Sql Server Connection Error in C# app

My objective was to make a database for general store. Information was very scattered about how to create database and why in that way. So i selected SQL Server Express.
Application was made in C# Visual Stduio 2012. Database was created with SQL management Studio. To connect the application with database i used serverexplorer in visual studio and assigned ./sqlexpress
Now the problem is that database works fine on my pc but when i deploy application on the clients pc it says
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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
Connection String Used connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=pos;Integrated Security=True";
Where i went wrong ? Is database also deployed with published app or i have to do some other steps ? Will i have to install SQL server on client pc too?
Note: The application is local. i don't need to transfer data via internet.
Let me know according to your experince what is the right way to attach,create and deploy database the right way for this small project because internet is full of guide but there is no where mentioned properly why the specific method is adopted etc
I solved the issue by download sql server management studio on client pc. I just made a backup of database form my management studio and restored that backup file from clients management studio. And now everything is working

System.Data.SqlClient.SqlException: Cannot open database requested by the login. The login failed

More of the exception
System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open database "BotDb" requested by the login. The login failed.
Login failed for user 'PIXELBOT\pixelbot'.
Hello
I made a console applicaiton in C# which uses a database with Entity framework.
The program works just fine on my own computer, but i need the program to run 24/7, so i rented a VPS through azure.
The problem is, that the program is not working on the VPS. The VPS is all new, and totally clean. The only thing i installed is Microsoft SQL Server 2014 LocalDB Express 64 Bit. I get the exception in the top of this post.
I really have no clue whats wrong, or what i should try to do. I've been searching around, and found other threads with problems like this, but none of the solutions work so far.
My connectionstring in the program if that is relevant:
<add name="BotDb"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=BotDb;User Id="usernameonvps";Password="passwordtovps";Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
Do you check MSSQL settings? Is the SQL Server authentication switched on? May be server allows Windows authentication only.

Database connection in windows azure

I have an N-Tier MVC4 project connected to an SQL server database.
The MVC4 project was created using the MVC internet application template in VS.
I'm trying to deploy this project to windows azure.
The website was added to azure with no problems the database was added with no problem as well.
When i browse to my website it's there but when i try to do actions related to the DB such as register/login/view a certain list i get the 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."
my development connection string is:
<add name="CarRentalDataBaseEntities" connectionString="metadata=res://*/CarRentalDataBase.csdl|res://*/CarRentalDataBase.ssdl|res://*/CarRentalDataBase.msl;provider=System.Data.SqlClient;provider connection string="data source=CONSTANTINE-PC\SQLEXPRESS;initial catalog=CarRentalDataBase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
my production connection string is:
<add name="CarRentalDataBaseEntities" connectionString="metadata=res://*/CarRentalDataBase.csdl|res://*/CarRentalDataBase.ssdl|res://*/CarRentalDataBase.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:ptuwjgqh7p.database.windows.net,1433;Database=CarRentalDataBase;User ID=XXXX;Password=XXXX;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
when i run the project in VS it works fine with no problems, which means that remote connections allowed to the database.
Seems that there something wrong with the connection between the website and the database in azure but i dont know what.
please advise what can i check in order to find the problem.
Thank you.
EDIT:
I just created draft MVC website that has a connection to a database and it worked - There is a connection to the DB in azure.
The difference was that this website wasnt N-Tier designed meaning it didnt have a business logic project.
In my original website (the one that i still didnt manage to connect to a DB) there is a business logic project and the database edmx file is on that project.
The publish to azure that i did was to the MVC website project that has a reference to the business logic(which has the edmx DB file).
Any suggestions on the latest findings?
Thank you.
Eventually I found that in the publish menu, in Settings under databases, from some reason, the connection string did not match the connection string in AppConfig and Azure management portal all though I performed a Clean-Rebuild on the entire solution before publishing.. Anyway mystery solved after almost a year!
On the firewall section of the portal for that server have you turned on access to Azure Services?

Categories

Resources