I have a number of .NET web apps on a Server 2008 machine that I'm trying to migrate to a Server 2019 machine, and some of them are giving me problems connecting to SQL Server 2016 instance on another server after moving them.
The error I get is
System.Data.SqlClient.SqlException (0x80131904): 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: 25 - Connection string is not valid) ---> System.ComponentModel.Win32Exception (87): The parameter is incorrect
This one is a .NET Core 2.2 web app. (.net core 2.2 server package installed on server) the connection string is like
"Server=mysqlserver\myinstance,3050;Database=Idea;Trusted_Connection=True;"
and is using Entity Framework/DbContext to connect.
The app in IIS has an app pool created for this app, set up the same as it was running on the old server: No Managed Code, Identity set as a domain user "domain\user".
Ping from new app server to database server works. Running SSMS as the domain user on the new server connects to the database fine and can view data through management studio.
So I don't know if there is something different in Server 2019 about the way it's trying to connect to SQL Server or what? I've been banging my head on this for a few days now. .NET 4.0 apps are also having the same errors trying to connect to other databases on this same instance.
The weird thing, is some other apps work fine, connecting to a different SQL 2016 instance on a different server, but they are also different .net versions, like older .NET 4.0 web apps, but they are running as app pools with different domain accounts for each app fine.
It seems all .NET Core or .net 4.0 web apps on this server are having trouble connecting to this one database instance from this server, but back on the old web app server they work fine.
Any ideas of anything else I can check?
Edit: I found the error is actually when connecting to a new SQL Server, even from the old app server it still gets the same error. I have 2 connections in this application, so I thought it was the first one, but it was actually the 2nd one. So it's something more to do with the new SQL server instance. Again, connections from SSMS work fine with this user, but not from the web app.
Edit2: After more testing, it's definitely something to do if there is Server 2019 in the mix. From 2019 app server to 2019 db server fails. From 2008 app server to 2019 db server fails. From 2019 app server to 2016 db server fails. From 2008 app server to 2016 db server succeeds.
Edit 3: I feel like I'm going crazy here. One of the apps, I tried to change ASPNETCORE_ENVIRONMENT to Development so that I could see more detailed errors on screen, so I set the appsettings.Development.json to the exact same as the appsettings.Production.json and then the connection works! Switching it back to production it gets the error again. The entire file is the exact same text. How does that even make any sense? I even tried explicitly setting the environment variable to Production instead of letting it just pick it up as the default.
Edit 4: I've solved half of the problems I've been having now.
For whatever reason, the connection string that I copied from one of the spreadsheets in the beginning had crazy hidden characters in it so that’s why it was saying the sql server didn’t exist.
I can’t see them at all in any editor and only found it by VS Code compare saying the line was different but not seeing any difference I broke it into chunks and found the spot. We found when we opened it in WordPad, that was the only place that would show it, see below.
I'm fairly sure the solution is to remove the instance name from the connection string...
"Server=mysqlserver,3050;Database=Idea;Trusted_Connection=True;"
Refer to these questions question1 & question2
"It's not necessary to specify an instance name when specifying the port."
Problem was due to hidden characters in the connection string. See my Edit 4. Other problems leftover were unrelated.
Try to create an user in the database using the same AppPool name that you used to configure your application in IIS.
Here's a "how to" create the user in the database:
https://engram404.net/grant-sql-permissions-to-iis-apppool-user/
It worked for me.
Otherwise here's an "why" it happends:
You're telling in your connectionString that it will be using an trusted connection "Trusted_Connection=True;"
If you do not want to create the user as described earlier, you should remove the Trusted_Connection=True; and use your connection string like this:
Server=mysqlserver\myinstance,3050;Database=Idea;User Id=SetYourUser;Password=SetYourPassword"
I know you happened to solve this, but still..
You can't connect to a MySQL database with System.Data.SqlClient - It is configured for an SQL database, not MySQL
You can find the MySQL Data Connector Here. (You can otherwise download it from Nuget)
If you downloaded it from Nuget, then skip this step.
After downloading the package, you can add it as a reference in your project, by right-clicking the References item in the Solution Explorer, then click Add Reference... - The Reference Manager window will then open. Click the Browse item in the left-menu, then click the Browse button, and navigate to the directory of which the package was saved.
Now, after successfully downloading and installing the package, add this line to your code:
using MySql.Data.MySqlClient;
The correct syntax for connecting to your MySQL Database, using MySql.Data.MySqlClient;, would be:
string connectionInfo = #"Server=localhost;Database=your_database;User ID=root;Password=123456";
So, overall, your code would look like:
using(MySqlConnection con = new MySqlConnection(connectionInfo))
{
con.Open();
MessageBox.Show("Successful Connection!");
}
(Code / part of answer is derived from here.)
I hope I could help anyone else with this problem :)
Related
I am trying to connect my .NET project to SQL Server developer edition 2019 (named instance), but it always throw the error:
Login failed for user 'sa'.
It however connects perfectly through SSMS without any issues.
I tried all possible things as suggested over internet but could not make my website run, is it actually the named instance which causes this issue, or it's issue at large.
I am able to connect my project if i connect to my database located on the remote server. but the one on my local machine just does not works.
Here's how my connection string looks like
server=[machine]\\[instance]; Integrated Security=SSPI; initial catalog=[database];Connection Timeout=1000
I also tried the same with SQL Server authentication
server=[machine]\\[instance]; uid=sa; pwd=[password]; initial catalog=[database];Connection Timeout=1000
None of the above worked out.
Ok, SSMS can connect using direct.
However, from asp.net, you in most cases have to turn on networking.
For reasons of secuirty, by default, your local instance of SQL server express has this feature turned off by default.
so, you need to do two things - and this was NOT required with older versions of sql server, but for recent versions - starting around 2012, you need to:
Turn on the browser service. This "translates" the sql "instance" to your IP address. (on startup, sql server assigns a IP address to that instance, but it will NOT be the same IP as your computer). But, the NAT translator thus has to be running.
That IP translator? It called the SQL browser service, and as noted, you need to have that running now, where as in the past you did not. That is this setting - launch the sql configuration, and you see this:
So, make sure the SQL Server Browser service is running - it is NOW required and as noted was not in the past.
Next up:
You need to turn on networking for SQL server. SSMS (sql studio) is able to connect using memory pipes - applications using the sql server provider in .net cannot!!!
So, once you ensured and checked the above browser service is running?
Then click on SQL server Networking Configuration, and turn on network connections (again, they are not turned on by default). That is this:
Now, after doing above, you probably should re-start sql server, and then the browser service. (just right click on sql server again, and choose this:
So, while SQL studio can connect using shared memory, .net and asp.net sites can't.
So, start the sql browser service - it is required to "resolve" the instance of sql server. in most cases, that will be SQLEXPRESS.
And consider using the built in connection builder in Visual Studio - it will not only build the connection string for you, but will also test the connection - and this can be done long before you even run any code.
I created a VS setup project in which I added a custom action to create an SQL Server database. I used the code from here https://daoudisamir.com/install-sql-server-database-with-visual-studio-setup/ that is itself a c# adaptation of the Microsoft Docs code here: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/49b92ztk(v=vs.100). I made a little adaptation - the entire connection string is in the code:
string connStr = "Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = master; Integrated Security=True";
This way, I'm not using any dialogue during the installation and it starts the custom action without any input from the user.
But when trying to the installation file, I get each time a "Failed to connect to server (localdb)\MSSQLLocalDB" error. Some of the times, the inner exception message that I have in the log file said "Login failed for user 'NT AUTHORITY\SYSTEM'", sometimes just "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found".
I know for a fact it's not the code in the custom action - I copied it to a form and added a button for the db creation via the application itself - no problem, no connection problem, db created right away.
I must add, that previously, I had a prerequisite for installing SQL Server Local DB during the application setup. That ended up miserably - it blocked me from having ANY access to the server - I had to uninstall everything VS and reinstall again before it went back to normal.
It seems to me that for some reason, instead of using the default windows user that is running the setup file, for some reason it switches to a different user. Is running the code via an msi file causing this? How can I solve it? It's been 2 full days, I read many threads and didn't find the answer.
Thank you for any help.
Edit: The LocalDB documentation states specifically that
An instance of LocalDB owned by the built-in accounts such as NT
AUTHORITY\SYSTEM can have manageability issues due to windows file
system redirection. Instead use a normal windows account as the owner.
HOWEVER, I didn't choose to run under SYSTEM. For some reason it switches to this user, while after the installation when the application is running with the same connection string, it uses the normal Windows user as expected.
Why is it switching to this user? That's what I'm trying to figure out. I have windows 10 if it matters.
I've just start working on an ASP.NET Web Application in Visual Studio 2015 and SQL Server 2014. Somewhere, I need to connect to my local database and I use this:
ConnStr = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=my_db;Integrated Security=True";
It works fine when I run the application through VS in my browser. I can access the db and do whatever I want to do. But, when I publish my application in IIS Web Server, and then I open it in browser, it still works OK until I have to access the db. At that moment it throws SqlException:
System.Data.SqlClient.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. .
Maybe it is a server name problem and I should use an IP and a Port instead of that name, but I'm not sure because I don't have experience on this.
You are correct: You should use server name and/or IP in your connection string.
Using something like "local" or "localhost" means that your code is not portable. Another option would be to store your connection strings in two separate config files - one for your local copy (for development and troubleshooting) and one for your server (for portability). I have two config files in my solutions:
Web.config
WebServer.config
Then, when I deploy to the server, I just delete Web.config and rename WebServer.config to Web.config. It's totally portable and you'll never have connection string troubles again!
Also noteworthy: you're not including credentials in your connection string, which means that you're using windows authentication when connecting to SQL server. When debugging through visual studio, the application will run as you - and if you have the needed permissions, it will work. However, when running in IIS, it won't be running as you (at least, it SHOULDN'T be) - so you could run into issues there, as well.
EDIT
This link might be useful for you: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
Your connection string should look like this:
Data Source=192.168.1.10,1433;Initial Catalog=YourDatabaseName;User ID=SqlUserName;Password=SqlPassword;Connection Timeout=60; Max Pool Size=3000;
(you can set max pool size and connection timeout to whatever you want - or omit them entirely.
Also, regarding your windows issues - you need to make sure that the windows account IIS is using has permissions to traverse your network and reach your SQL Server instance. That said, I suggest that you use a SQL account instead of windows authentication.
Since you are using integrated security in connection string you will have to modify the Identity of App pool under which your application is running.
The Identity will be your windows username and password.
To change the username and password you need to go the advance settings of the app pool and process model you can see identity where you can add your windows credentials
Hope this helps
There are two problems in you connection string:
"Integrated Security" means you are using the native windows system for authentication. Similar to opening SSMS on the database using your Windows password. IIS is now trying to connect to the database, and connection string is telling to use the process that IIS is running under.
You can create a non-windows user in SQL Server and put the credentials into the connection string. Or you can grant the IIS user DB privileges. Or you can a lot different things here, but theses are the easiest to get you moving.
THe second problem in the connection string is the data source. Is there SQL Server on you local machine? If so that's why it's not working. Try to run your app in VS but against the remote SQL Server. That should be your next step.
The problem was that I thought that SQL Server was installed automatically with VS or at least with SQL Server Management Studio. BUT NO. So, as far as I understand, till know I have not worked with a real SQL Server. When I checked SQL Server Configuration Manager there were nothing running at SQL Server Services and so I realized that I was missing something.
Then, I installed SQL Server Express and build my db there. Now it is working fine even when I publish it. The connection string is
Data Source=.\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
It can also be:
Data Source=localhost\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
I've an oracle server, already installed on a remote server.
I've installed oracle latest provider, to use them in visual studio with entity framework.
But when I'm trying to connect to the server, I got this exception:
ORA-12560: TNS:protocol adapter error.
I'm really new to the oracle world, and I cannot find what is the problem or even how to debug it.
I saw that listeners are ups, by doing a lsnrctl status I've my listener on the port 1521.
But, I saw that i've the security like this: "Security ON: Local OS Authentication", but since I've no common users between the server and the client, can it make somes troubles?
Should I have some specials rights on my user? In local, I can connect myself with the sqldeveloper tools.
Any help would be greatly appreciated
EDIT
Some more informations:
The server runs under windows, it has the OracleXETNSListener service started. I forgot to mention, but the server firewall is off.
Edit 2
I tried to download the oracle sql developer on my workstation, and I connected myself with exactly the same informations.(I just saw that in fact sql developer use a "base" connection instead of "TNS", which seems to be used by the EF?
I ended by using the devart connector: http://www.devart.com/dotconnect/oracle/
it worked for me directly in all mode(Direct or normal). If only I did tried it this morning, I will have loosed less time.
Are you using your tns names via AD or locally on your machine? If it's local then you'll need to make sure that you have the correct connection descriptors listed in the tns names file located in the default location - this depends on Oracle version.
There are known issues around entity framework and connecting to remote instances, but these can be overcome with a l
Using the Oracle Provider, your connection string should look something like this:
Data Source={serverAddress}/xe;User Id={UserName};Password={Password};
Example:
Data Source=localhost/xe;User Id=scott;Password=tiger
I suggest you to always mention the Oracle Instance Identifier (which in the Express Edition is always xe), so you don't have to rely on your tns config settings.
I have a console app that needs to connect to a remote sql server 2008 instance....
this particular line throws an error even though i have access to that database...
connection.open() is the line that is throwing the error...
Make sure that Sql server 2008 instance available at your system where you are running your application.
Atleast client version of Sql server 2008 should be installed on development machine.
Check connection string and server system also allow network connection.
Make sure the connection string in you console app uses the same credentials and settings that you use to successfully connect in management studio.
Are you using Windows authentication in one place and a SQL login in another?
Have you been able to connect to this remote server before? If you have been able to connect previously, but cannot do so now, that may be an indication that a backup copy of the database, from another server, has been restored to that server.
When you write that you "have access" to that database, how do you connect? Close your copy of SQL Server Management Studio and re-open it; when you get the Connect dialog, be sure to enter the user credentials that your console app uses to connect. Do you see your database? Are you able to execute stored procedures, run SELECT statements, or otherwise do what you would expect?
If you do not see the database--or if you cannot do the activities you expect, you have probably restored a database from another server. This is very common--a database developer is working on the next revision of the database on his development machine; when it comes time to deploy he backs up the database, copies it to the server, and restores the database. Now he can't connect--and can't understand why.
The reason is that the Login and User are presented in the SSMS UI as strings--but in fact they are integer IDs. You will have to do a bit of scripting to delete the invalid User record from the database in order to assign it to the new server's Login.
If this sounds like your problem, respond--I'll check back in a bit, and follow up with more help.
JM