Error Database schema could not be retrieved for this connection - c#

Edit: if you are downvoting can you at least tell me what Im missing so I can improve my question?
I have a website which uses a SQL Server database. It works fine when building both debug and release.
But if I add a new SqlDataSource to my project and try to configure it in design view, I get this error:
Database scheme could not be retrieved for this connection. Please make sure the connection settings are correct and that the database is online.
Failed to retrieve data for this request.
Failed to connect to server---------.
Login failed for user 'xxxxxx'.
Even if I configure the connection string with the connection-guide, test the connection and save, it fails afterwards when trying to configure the datasource.
Connection string:
Data Source=SERVER\SQLEXPRESS,61180;Initial Catalog=DBNAME;User Id=USERNAME;Password=PASSWORD;
So to clarify: The connection string works fine when running the website in both debug and release, and if I connect using SQL Server Management Studio, but the only place it doesn't work is when I try to configure the datasource in design view.

After a lot of trial and error I found out that I had to put Persist Security Info=True; into the connectionstring to get it to work.
First time in my 10+ years of programming that I encountered this problem.

Related

ASP.NET MVC: moving from SQL Server Express to Azure

I'm sure this has been asked before, but I'm not seeing anything in the similar questions. :(
I've got an ASP.NET MVC application I have developed locally that has been using a SQL Server Express database. In Azure, I recreated the database and taken the connection string it provided and updated the connection string in web.config of my app to use the new database.
When I publish the app to Azure, there doesn't appear to be any issues. However, when I hit a page in the app that accesses the db, it throws an error complaining it can't find the SQL Server Express database. Below is the main snippet from the error:
SqlException: 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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
So it seems like it's ignoring my connection string and still looking for a locally stored SQL Server Express database instead of using the Azure database I specify in the connection string. Any ideas why this might be? My guess is that I have missed something pretty basic (my usual guess when something that seems like it should be simple doesn't work. :) )
Connection strings per below request:
Local:
<add name="FCDbContext" connectionString="data source=(localdb)\MSSQLLocalDB;initial catalog=FormCenterResponder;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
Azure Contection String:
<add name="FCDbContext" connectionString="Server=tcp:***azuredbserver here***,1433;Initial Catalog=***azuredb here***;Persist Security Info=False;User ID=***username here***;Password=***password here***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
And configureservices per another request (it's pretty much defaults):
public void ConfigureServices(IServiceCollection service)
{
services.AddControllersWithViews();
}
Ok, mark this one up to being a asp.net/c# noob. I'll go ahead and post (the embarrassing) answer, though, in case others find themselves in my position.
A co-worker had a look at the code and found an excellent tutorial here. He was able to figure out what I had done wrong.
Basically, I had never registered the database context in Startup.ConfigureServices. I think this is why someone (who has since deleted their question) asked me to post that section of my code. Then that context can be read in by the controller files granting them the info they need to get access to the db.
App is now working as it should.

Connection to Oracle database not working

I am currently working on a project with someone. We are connecting to a oracle Database using the NuGet Package Oracle.ManagedDataAccess.Core.
We are connecting using a connection string that looks like this:
"User Id={this._dbUser};Password={this._dbPassword};Data Source={this._dbServer};"
The Parameters we pass on are correct we checked multiple times.
At first I got a Connection Timed out error which we fixed by adding persist security info=false;Connection Timeout=120; to the connection string.
At first it seemed to be working but then we encountered a new error.
This time it was saying:
Oracle communication: Connection to server could not be established or connection string not parsed
(Might not be 100% accurate because I had to translate it from German to English)
We could not find a solution for this error but we discovered that the error only gets thrown when we run the code on my machine. His is doing totally fine and can connect without any problems.
Could it be that I have some settings set on my machine that would prevent me from accessing the Database?

Unable to connect to master or target server "ServerName"

Have some troubles with Unit Test and Dackpack. This is the full error name:
DeploymentFailedException: Unable to connect to master or target server 'DBTests'. You must have a user with the same password in the master or target server 'DBTests'.
I have 3 strange things :
I can publish my Database on Server (tests have the same connection string)
I can log in in my site and I don't have troubles with DB (using IIS)
Test logic: Test database created when I init my tests, and it is really created!!!!
But all tests have "Error".
I upgrade my SSDT. I reinstall MSSQL Twice. And I set compability level(130). And what is so funny this is my Connection Strings when I try to debug my tests:
"Data Source=ServerName;integrated security=false;Initial Catalog=DbTests;User ID=kek;Password=kek;Connection Timeout=60;MultipleActiveResultSets=True;Max Pool Size=1024"
That is TestDatabase connection string. And she is absolutely fine because I TESTED this connection string.
This is masterDatabase connection string:
Data Source=ServerName;integrated security=false;Initial Catalog=master;User ID=kek;Password=kek;Connection Timeout=60;MultipleActiveResultSets=True
And the main question what I have: Why Error say what 'DBTest' - this is a server ?? I mean, this is DB, not server.
Maybe someone knows about that, idk. That is so stupid and I can't understand what's wrong.
UPD: I solve my problem. Installed SQL Server 2016.
P.S. Ofc I can log in on the server with my "kek", "kek" credentials in Management Studio.

Azure Cloud Service unable to connect to SQL Database on write operations only

I have developed a web application in lightswitch. There is an HTML lightswitch client that connects to a server (C#) and this server gets the data from an SQL Database.
The application is working correctly in my local machine, the problem comes when I publish the solution to a Cloud Service in Azure. Then, the server connects to the database correctly and can read all the information from it, but when I try to update the database from within my application (insert or update rows) the application freezes and it promts the following error:
Unable to connect to SQL Server database.
Inner exception message:
Unable to connect to SQL Server database.
Inner exception message:
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)
I have checked the connection parameters and all of them are correct, this is the connection string in my web.config:
<add name="My_Data" connectionString="data source=tcp:{MyServer}.database.windows.net,1433;initial catalog={myDatabase};user id={Myuser}#{MyServer};password={MyPassword};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
I have seen other questions with the same issue, but all of them were because the database or the server was not available. I have no problem connecting to the server because I can query my database with no problems.
The error comes when I try to write in my database, either because I click on the save button of a lightswitch screen or because I do it by code in the server:
serverContext.DataWorkspace.My_Data.SaveChanges();
If I comment the previous line, the error is not prompted.
I have also checked the user permissions in the database and this usser is a db_owner, so there should be no problem with that.
Anyone has ever faced a problem like this?
I found the solution!
After a deep research, I discovered that Azure server is adding a connection string to my application (I don't know how nor why). This connection string is inherited from somewhere and it is pointing to a local instance of SQL Express. Obviously, this instance does not exist, so after a timeout the application throws the error shown above.
I don't really know why this is happening nor why only happens when I try to write in my database. The solution I found is to add the following in the connection strings part of my web.config:
<clear />
With this, we delete the inherited connection string. And now, we have to set the connection string of the LocalSqlServer (which will be our connection string, the one that points to our remote SQL Server in Azure):
<add name="LocalSqlServer" connectionString="Data Source={SERVER}.database.windows.net;Initial Catalog={DATABASE};Integrated Security=False;User ID={USER}#{SERVER};Password={PASSWORD};Encrypt=True" />
Now, everything works fine. I hope this can help anyone who faces the same problem than me and I would really like to know why this happened, so if anyone knows it, please tell me :)

Connect timeout, database failover not working

I have implemented database mirroring on Azure with a witness, using SQL Server 2008. The mirroring / failover works on the SQL Server side. The problem I am having is that once a failover has occurred my asp.net Website does not connect to the new principle database and is still trying to connect the the old principle.
I have tried adding a 60s Connect Timeout to my connection string as suggested in this post. However this does not have any effect. And all I end up getting is the following error after around 60s:
Request timed out.
Does anyone have any other suggestions?
Any help much appreciated!
EDIT
My connection string is:
data source=server1;Failover Partner=server2;initial catalog=DBNAME;User Id=WebsiteUser; Password=******;Connect Timeout=60;multipleactiveresultsets=True
did you add the failover partner to the connection string?
see http://blogs.msdn.com/b/spike/archive/2010/12/08/clarification-on-the-failover-partner-in-the-connectionstring-in-database-mirror-setup.aspx for example and clarification

Categories

Resources