I have a small application which is trying to import data from a MySql database into a SQL Express database. This is under development in Visual Studio 2013 on Windows 10, using Entity Framework Database First to model the MySql database, connect to it and retrieve the data.
The software is being developed TDD, so I have an NUnit test which is checking that the application can successfully connect to the MySql database and retrieve data from it. One test is very simple, as follows:
[Test]
public void MySqlContext_CanConnectToLegacyDb_OpensWithoutErrors()
{
// Arrange
using (MySqlContext legacyRepository = new MySqlContext())
{
bool exceptionThrown = false;
List<string> exceptionMessages = new List<string>();
// Act
try
{
legacyRepository.Database.Connection.Open();
}
catch (Exception ex)
{
exceptionThrown = true;
exceptionMessages.Add(ex.Message);
while (ex.InnerException != null)
{
ex = ex.InnerException;
exceptionMessages.Add(ex.Message);
if (ex is SocketException)
{
exceptionMessages.Add(string.Format("ErrorCode={0}", (ex as SocketException).ErrorCode));
}
}
}
// Assert
StringBuilder errMsg = new StringBuilder("Exception was thrown: ");
foreach(string em in exceptionMessages)
{
errMsg.AppendLine(em);
}
Assert.IsFalse(exceptionThrown, errMsg.ToString());
}
}
Ie. it is simply checking that it can open the connection to the MySql database without any exceptions being thrown.
This test is failing. The attempt to open the MySql database produces the following exception:
Result Message: Exception was thrown: Unable to read data from the
transport connection: An established connection was aborted by the
software in your host machine. An established connection was aborted
by the software in your host machine ErrorCode=10053
Expected: False
But was: True
MySql is running on the same machine as this test and of the application that it is supporting. It is in use by other software on the machine, including MySql Workbench and the Server Explorer of the same Visual Studio instance, without any connectivity problems. I have checked and re-checked the connection string being used to access the database, and it is the same one that was generated by Entity Framework when it generated the model for the database.
Based on what I've read about this exception online, I have made a thorough check of all of the security settings on Windows 10, I have reviewed MySql logs and other logs, including the Windows event logs. I can find nothing in any log which hints at what is aborting the connection with MySql. Fiddler does not show anything when the offending line of code fails, so I can see no evidence of anything trying to talk to MySql via the localhost/127.0.0.1 connection.
I've tried disabling those settings that I can find, and disabling by BitDefender anti-virus software, and none of those actions has any effect on this error.
What else can I check, to find out the cause of this error? Could it be a security setting within Windows?
Some further information:
Prompted by something I read elsewhere about EF Database First, I tried deleting the MySql model from my solution and re-adding it, to see if this changed anything in the connection string. This time through, the ADO.NET Entity Data Model wizard dies, with no errors, when I click Next on the Choose Your Data Connection screen (3rd screen). When it does this, the Server Explorer connection to the MySql database closes (the icon shows a red cross, when it was showing a green plug).
I'm now thinking that there is some configuration problem, somewhere in my solution. But how to diagnose the problem? I get no error messages, when the wizard dies, and the Output window shows nothing.
Another update:
I think the problem might be down to MySql configuration issues. I appear to have 2 or possibly 3 "my.ini" settings files for MySql, and I suspect that the one being maintained by the MySQL Workbench is not the one that the server is actually using for its configuration. Why do I think this? Because, when I enabled logging for various items that are not logged by default (why not?), no change in behaviour happened. MySql did not begin writing log files - until I copied the my.ini file that had been changed by the utility (C:\Program Files\MySQL\MySQL Server 5.5\my.ini) over the file that was in C:\ProgramData\MySQL\MySQL Server 5.5. After I did that, MySQL started logging stuff, but then I could not connect to the server properly via the Workbench.
I am investigating further, and will post what I find here, in case anyone else has a similar problem.
Related
Im learning .net now and following along the course,my goal is to create CRUD app so i need a database to work with it, i have succesfuly created initial migration but database-update doesnot work
(provider: SNI_PN11, error: 50 - Local Database Runtime error
occurred. Cannot create an automatic instance. See the Windows
Application event log for error details.
im getting this error if connection strings look like this
"ConnectionStrings": {
"QuotesAppContext": "Server=(localdb)\\mssqllocaldb;Database=QuotesAppContext-811c920d-aeb7-4d04-8ba6-5c9a48d8b492;Trusted_Connection=True;MultipleActiveResultSets=true"
}
i have installed both sql server and sql management on windows 11 without problem
and if i change connection string to this
"ConnectionStrings": {
"QuotesAppContext": "Server=(localdb)\mssqllocaldb;Database=QuotesAppContext-811c920d-aeb7-4d04-8ba6-5c9a48d8b492;Trusted_Connection=True;MultipleActiveResultSets=true"
}
deleting \ problem now is
Unable to create an object of type 'QuotesAppContext'.
so it seems like an endless circle to me, thanks you for your help
write database-update and get database instance working
I find the answer which have worked for me,find this folder in regedit
"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances"
and delete everything inside of it,and after that run Update-Database command again everything should work fine
P.S
i have tried reinstalling sql server,adding ports, but this solution worked for me
I am attempting to connect to an IBM DB2 database from a .NET 5 console application. This works fine against DB2 LUW in a local Docker instance, but fails when connecting to a z/OS mainframe instance.
Server:
IBM DB2 v11.5
z/OS
Client:
.NET 5 console application
IBM.Data.DB2.Core (v3.1.0.400)
IBM Data Server Driver (v11.5)
Error:
IBM.Data.DB2.Core.DB2Exception (0x80004005): ERROR [57017] [IBM] SQL0332N Character conversion from the source code page "" to the target code page "" is not supported. SQLSTATE=57017
at IBM.Data.DB2.Core.DB2ConnPool.Open(DB2Connection connection, String& szConnectionString, DB2ConnSettings& ppSettings, Object& ppConn)
at IBM.Data.DB2.Core.DB2Connection.Open()
at <my code>
Connection string:
Database=<redacted>;User ID=<redacted>;Password=<redacted>;Server=<redacted>:448;Persist Security Info=True;CurrentSchema=<redacted>;Connect Timeout=30
Opening connection:
var connection = new DB2Connection(connectionString);
try
{
connection.Open();
}
catch (DB2Exception e)
{
logger.LogError("Unable to access DB2 instance");
logger.LogError(e.ToString());
throw new DbAccessAcception(e);
}
The DB user I am testing with is already in use by another .NET program to connect to this database, though that app is older (.NET Framework 3.5).
What I've tried:
Setting the code page to 1200, 1208, or 1252, using both the DB2CODEPAGE environment variable as well as the connection string CodePage parameter; nothing changed the error message
Verified all software (DB2, DB2 DSDRIVER, .NET provider) are v11.5
Verified I can connect and run queries with db2cli.exe
Now what?
Is there somewhere I can/should be setting the DB2 server type? Eg, z/OS vs LUW? Note that I'm not using EntityFramework, just directly executing Commands on that Connection object (though the error comes before then).
We have solved the problem. There were two changes to make, which definitely complicated things.
We did need to set the DB2CODEPAGE environment variable to 1208
We were using the CurrentSchema connection string parameter to set the prefix for our SQL queries, but the prefix wasn't the actual schema, which broke the connection even when the right codepage was set, without a useful error message on the client. We removed that parameter and manually set the table prefix on our SQL queries.
Making both those changes got the application working.
Connecting to the local Docker instance had worked because I had set an actual schema for that prefix, not realizing the prod instance was configured differently.
We use entity framework to read from an existing database.
This is a simplified version of our code.
using (my context context = new mycontext())
{
if(context.Database.Connection.State == System.Data.ConnectionState.Closed)
{
_logger.Info(" Opening the connection to the database");
context.Database.Connection.Open();
}
context.Configuration.LazyLoadingEnabled = false;
IQueryable<mymodel> people;
people = context.People.OrderBy(x => x.Firstname);
_lstContacts = people.ToList();
if (context.Database.Connection.State != System.Data.ConnectionState.Closed)
{
context.Database.Connection.Close();
context.Database.Connection.Dispose();
_logger.Info(" Connection to the database Closed");
}
}
It works 100% of the time, but...
On our UAT environment we can see failed connections to the Microsoft SQL server with the error:
Login failed for user "my user". Reason: Failed to open the explicitly
specified database "null". Client my IP.
For us, these are ghost connections because at the time when we see the errors in the SQL server, our code is not executed.
Initially we didn't close and open the connection explicitly, we just added it trying to control when EF open and closes the connection, but it didn't fix the issue.
Our connection string is using the following format:
<add name="MYCN" connectionString="metadata=res://*/CVs.Cvs.csdl|res://*/CVs.Cvs.ssdl|res://*/CVs.Cvs.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver\;initial catalog=mydatabase;Integrated Security=;User ID=myuser;Password=XXXXXXX;MultipleActiveResultSets=True;App=EntityFramework"/>
As you can see, we are specifying the database in the connection string and our user only have access to our database, so we understand the error when EF doesn't include the database in the connection string, but we don't understand why it's trying to perform these connections.
We know the connections are coming from our application, because we are the only one using that specific user, the IP is the IP of our server, and because the logs in SQL server tell us that the application is "EntityFramewrok"
I didn't personally see the error before, but researched for you and seen that many people suffered from the same problem discussed here: https://blogs.msdn.microsoft.com/sql_protocols/2006/02/21/understanding-login-failed-error-18456-error-messages-in-sql-server-2005/
I read all the messages in the website specified, and here are the solutions offered and at least one other user confirmed that it worked. You might not use 2005 as you didn't specify your version in your question, some solutions I believe will still work for you. Try the list below.
Solution list:
1) Please check the state number of this error and search solution by the state number in addition to the message, might give your more accurate solution proposals. Most common states are listed:
All state-error descriptions you can find here: https://sqlblog.org/2011/01/14/troubleshooting-error-18456
2) Make sure the username and password are correct.
3)
Logon to SQL Server using windows authentication.
Right click in the query window that appears and select "Open Server in Object Explorer"
Go to object explorer and open the "Security" folder and then the "Logins" folder.
Double-click the "sa" user and change the password. Also, like another user mentioned above, untick the "Enforce Password Policy" in
addition to resetting the password.
4) Try to change the password and turn off the policy, and try with new password.
exec sp_password #new = ‘sqlpassword’, #loginame = ‘sa’
alter login sa
with password = ‘sqlpassword’ unlock,
check_policy = off,
check_expiration = off
5) Run your application/browser and SSMS (if you work on it) in administration mode.
6)
Open Windows Services
Configure Microsoft Single Sign-on Service to use the proper account
Open Central Administration >> Operations >> Manage settings for single sign-on
Configure properties to use the same account used for Microsoft ‘Single Sign-on Service
7) Go to Sql server configuration manager and Enable TCP/IP and named pipes
8)
go to sql server
right click on server, choose properties
click on security
on server authentication, enable SQL Server authentication
These might help:
https://www.wikitechy.com/errors-and-fixes/sql/login-failed-error-18456-severity-14-state-38-reason-failed-to-open-the-explicitly-specified-database
https://dba.stackexchange.com/questions/90445/login-failed-for-user-error-18456-severity-14-state-38
I think this is just an access issue for myuser in the UAT environment. Just make sure myuser has access in the UAT environment for UAT database and you should be good.
I am working on application which uses Sybase database and entity framework for accessing database. I am trying to make application to open connection itself and close it. I created model using Sybase database file and now connecting to it to get data. But when I try to get data I get exception "The underlying provider failed to Open".
Here is my code.
var connectionString = metadata=res://*/SampleModel.csdl|res://*/SampleModel.ssdl|res://*/SampleModel.msl; +
provider=iAnywhere.Data.SQLAnywhere; +
provider connection string="DBF=D:\SampleDatabase.db;UID=DBA;PWD=sql"
var dataContext = new SampleContext(connectionString);
var contacts = dataContext.Contacts; --> Here I get this exception.
Note: If I create a DSN in ODBC and use DSN instead of giving filename it gives me Not connected to a database exception. If I open this connection and do not close it, then it works fine. But I don't want to create DSN entry and open it manually. I want my program to do this.
There was problem with my version of Sybase. I was using 3840 build of Sybase 12. I removed it and installed 3769 version and it worked fine. On different forums I found out that latest (3840) is having problems.
We're writing a multi-tenanted Silverlight application. The user logs in using Windows Forms authentication and we pull the connection string for that user from a table. The string was of the form:
Data Source=1.2.3.4; Initial Catalog=database; Persist Security Info=True; User ID=######; Password=##########
This was working fine. We then decided that setting the application name, for reasons outlined here would be a good idea:
Data Source=1.2.3.4; Initial Catalog=database; Application Name=application; Persist Security Info=True; User ID=######; Password=##########
However, now the connection isn't being made so we're getting errors. This is only failing on the deployed site. Running via Visual Studio is apparently unaffected. There have been no changes to the code that makes the connection. I'm willing to believe that there should be changes, but I haven't been able to work out what they should be.
The connection is being created like this:
public class OurDataContext : DataContext
{
public OurDataContext()
: base(SessionCache.OurConnectionString)
{
....
}
}
SessionCache.OurConnectionString is the string as read from the database.
UPDATE
The change to the connection string may be a red herring. We only get this failure on one web server (the one I don't have direct access to). Adding the property to the database referenced by another web server (that I do have access to) doesn't reproduce the problem. This is leading me to the conclusion that the error is somewhere else. However, I'm still no nearer to solving this.
The actual error is an exception:
An exception of type 'System.ServiceModel.DomainServices.Client.DomainOperationException' occurred and was caught.
------------------------------------------------------------------------------------------------------------------
01/31/2012 14:18:53
Type : System.ServiceModel.DomainServices.Client.DomainOperationException, System.ServiceModel.DomainServices.Client, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
Message : Load operation failed for query 'GetUserSecurityConfig'. The remote server returned an error: NotFound.
Status : ServerError
ErrorCode : 0
Data : System.Collections.ListDictionaryInternal
Stack Trace : The stack trace is unavailable.
All the research I've done today on this returns some fairly basic things that shouldn't have changed:
System.ComponentModel.DataAnnotations, System.ServiceModel.DomainServices.EntityFramework, System.ServiceModel.DomainServices.Hosting and System.ServiceModel.DomainServices.Server not being copied to the \bin directory. I've checked and they are.
Errors connecting to the database server. I don't think this is the case as the forms authentication works. It is a different database on the server, but the credentials are the same.
The problem turned out to be due to the version of RIA Services installed on the server.
We'd recently changed the projects to include RIA Services via the NuGet package manager and this server already had RIA Services for VS2010 installed. When we sorted that out everything sprang back to life.
It's not clear why it took so long for this problem to manifest itself though.
We only get this failure on one web server (the one I don't have direct access to). Adding the property to the database referenced by another web server (that I do have access to) doesn't reproduce the problem.
Check webserver config and that the database is installed correctly.
looks like webserver config / db installion to me.