Can't connect with mysql server using c# at work - c#

Why i failed to connect to a free cloud mysql DB using C# connector from Nuget in my work office PC while i can connect just fine to the same server using mysql CLI tool?
The message i get when i fail to connect is "Connect Timeout expired.".
I also tried on my PC at home and i can connect just fine using exactly the same C# code.
Mysql CLI tool also work from home.
Could it be a firewall or other security measure in my work company network? Why it does not block the mysql CLI tool?
I also tried with a localhost mysql (using mysqld on port 51255) at work.
I managed to connect using mysql CLI tool but got the same error as before on c# code.
EDIT1 - Code included
EDIT2 - localhost attempt
private async Task<string> ConnectDB()
{
MySqlConnectionStringBuilder dbinfo = new MySqlConnectionStringBuilder
{
Server = "******",
Port = ****,
Database = "*****",
UserID = "*****",
Password = "*****",
};
using(MySqlConnection connection = new MySqlConnection(dbinfo.ConnectionString))
{
try
{
await connection.OpenAsync();
}
catch (MySqlException error)
{
message = error.Message;
}
if (connection.State == ConnectionState.Open)
{
message = "Success!";
}
}
return message;
}

We see the same C# code works at home, but not at the office. Therefore it's not the code; it must be something with the environment at the office. But you can connect at the office via the command line tools, so it's also not the corporate firewall, which would have a very hard time telling the the difference between the two.
What's left is something on the local PC at work. The local OS firewall and antivirus software both come to mind, and of the two the antivirus software sounds more suspect to me. One other option is DNS, where the DNS service at the office is resolving the host name for the database differently. But without more info this is just a guess. I suggest adding a lot more logging; right now we don't even know the full error message.
As an additional note, it's not common and considered very poor practice to directly expose a database to the internet. Additionally, the application should be well-connected to the database. The definition for "well-connected" has changed over the years, but "public internet" is not going to count. Typically, if you most host the database in another location you will have a web API endpoint in front of the database, where the server for this API can be in the same location as the DB and therefore well-connected.

Related

MongoDB throwing exception on resolving connection string (C#)

I'm building a winform app that initially connects to a MongoDB server using Driver 2.10.
When I try and connect on my own machine everything works smooth, but when trying to install
clients on other computers via a msi file, the app crashes and I get the following:
ERROR - List of configured name servers must not be empty.
Parameter name: servers
at DnsClient.LookupClient.QueryInternal(DnsQuestion question, DnsQuerySettings queryOptions, IReadOnlyCollection`1 servers)
at DnsClient.LookupClient.Query(DnsQuestion question)
at MongoDB.Driver.Core.Configuration.ConnectionString.Resolve(Boolean resolveHosts)
at MongoDB.Driver.MongoUrl.Resolve(Boolean resolveHosts)
at MongoDB.Driver.MongoClientSettings.FromUrl(MongoUrl url)
at MongoDB.Driver.MongoClient..ctor(String connectionString)
at PaladinFormV2.MongoCRUD..ctor(String i_database)
at PaladinFormV2.Paladin..ctor(Boolean onboarding)
at PaladinFormV2.Program.Main()
I'm connecting through a connection string as follows:
MongoClient client = new MongoClient("mongodb+srv://[name]:[pass]#mcsamples-nwups.mongodb.net/test?retryWrites=true&w=majority");
What am I doing wrong and what information am I missing here to solve this?
Thanks
So after a long time trying to figure out what went wrong here, I came to the conclusion that using the '+srv' inside the connection string tries to resolve the DNS server on which your mongo instance is running, for clients using this it may cause the app to crash.
My fix was using a different connection string that does not use this prefix.
The info on connection string was taken from here https://docs.mongodb.com/manual/reference/connection-string/
Edit your connection string
From
`MongoClient client = new MongoClient("mongodb+srv://[name]:[pass]#mcsamples-nwups.mongodb.net/test?retryWrites=true&w=majority")`
To
`MongoClient client = new MongoClient("mongodb+srv://[name]:[pass]#mcsamples-nwups.mongodb.net/test")`
Also make sure you have internet connection to the cloud server

Mono crashes while connecting to a sql server with a certain instance name

I created a sql server instance called 'abcd' on my computer but when I try to connect to it I receive a strange error. I am able to connect to my others instances with the same code. Is the instance name causing this error?
Here is my code :
string connectionString = "Server=192.168.1.185\\abcd;" + "Database=test;" + "User ID=sa;" + "Password=bob;";
IDbConnection dbcon;
using (dbcon = new SqlConnection(connectionString))
{
dbcon.Open(); // <-- crashes here
Console.WriteLine("Connected");
}
Crash
Mono does not support names pipes or shared memory for connecting to SQL Server. Please enable the TCP/IP protocol.
What's wierd is that I'm able to connect to my other instances. The only thing that changes is the instance name.
I'm using Xamarin.iOS 9.4.0.0, Xamarin Studio 5.10.1, Mono Framework MDK 4.2.1.102
In SQL server the enabled protocols are configured on a per-instance basis, so the most likely explanation is that the named instance you're trying to connect to doesn't have the TCP/IP protocol enabled.
You can check this by opening the Sql Server Configuration Manager tool and going to SQL Server Network Configuration -> Protocols for {instance name}. Make sure that the TCP/IP protocol is enabled.

SQL Server Connection Issue - I can connect but other users can't

[edit] so not sure what happened, but we ended up resetting the server and turning off/on TCP/IP and Named Pipes and after a restart and updating the settings everything started working again. weirdest thing. anyways thanks for the help guys.
I'm building a C# WPF application for my job, and I'm getting a weird problem that I've been trying to figure out for the past week. The application connects to the server and imports several tables on start up. So I built it out and was testing it with no issues, but when i pass it to our testers, and everyone is getting the following errors:
Provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
The users are able to connect to the server through other methods (SSMS/Excel VBA), but just through the application it won't work.
I've checked the following:
Remote connections enabled
TCP/IP connections enabled
Firewall settings are the exact same across all users (me included)
application is compiled as 32 bit (saw this in another thread)
We're using SQL Server 2008 and I've tried several connection strings/methods.
below is the code I'm using to connect:
public void Open_DB_Conn(string Connection_Str)
{
try
{
Sql_Conn = new SqlConnection(Conn_Str);
Sql_Conn.Open();
}
catch (Exception e)
{
MessageBox.Show(string.Format("Error Message:{0} Conn String: {1}",e.Message,Conn_Str));
}
}
Below is my connection string (this is just one of many iterations I've used trying to get it working):
Data Source=IP Address;Initial Catalog=DB_Name;User ID=LOGIN;Password=PWD
Anyone know why I would be the only one able to get it to work and that the users are able to login to the server using other applications without a problem? They use it for logging their excel VBA scripts and there aren't any issues there.
Try this - it may be your answer
"The error is reported by client library. While your server is listeing on remote TCP, client will still try TCP and NP connection in order. So the error client behavior is expected. From what you have described, I believe that even though you enabled the remote TCP connection on the XPSP2 machine, you didn't make the TCP listening port an exception of XPSP2 personal firewall. You should follow steps below to resolve this issue.
check the SQL Server Errorlog to make sure SQL Server is now listening on TCP/IP and confirm which port it is listening on. Usually 1433. In the Errorlog, you will see several lines that discuss what SQL Server is listening on. Below is an example:
2006-01-04 01:41:07.65 server SQL server listening on 10.254.1.150: 1433. <--Shows the IP Address and the port.
2006-01-04 01:41:07.65 server SQL server listening on 127.0.0.1: 1433. <--Shows another IP Address and the port.
2006-01-04 01:41:07.69 server SQL server listening on TCP, Shared Memory, Named Pipes.
2006-01-04 01:41:07.69 server SQL Server is ready for client connections
2, Make sure on Windows XP that the firewall is not blocking that port.
3, go to your client machine and run the client network configuration tool (cliconfg.exe) Make sure TCP/IP is enabled, click properties and make sure the port number is the same one as SQL Server is listening on. Here you can enable NP or disable client NP as well.
Once both the client and the server are using TCP/IP with the same port number and the firewall on server machines is not blocked, you should be able to connect.
Hope this helps."
(Ref: http://social.technet.microsoft.com/Forums/sqlserver/en-US/c488cf76-2515-440f-b3f8-9cfad689c5b6/named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server?forum=sqldataaccess)
You have to configured your SQl server so that other IP can connect it for that you have to gone through mentioned link
Configure SQL server
What authentication are you using for the SQL Server? Windows Authentication or SQL Server authentication? My suggestion is to first turn on SQL Server authentication and use the sa\password to connect to the server. If you are successful, then ask the others (users of your application) to try with the same connection string. Let me know what you find out.
Be sure that the port specified in:
Data Source="IPAddress,port";Initial Catalog=DB_Name;User ID=LOGIN;Password=PWD
matches the port on your SQL Server. You can check that by going on SQL Server COnfiguration Manager and viewing TCP/IP properties.
EDIT :
It is also the case the port defined by blocked by an external firewall. And the rest Applications use other ports. Try to find out which port you can use (if indeed the are restrictions to your network)
Make sure your SQL Server instance is properly configured to use TCP using Sql Server Configuration Manager.
It is by default disabled in SQL Express, as show below.
I'd like to know more about your "Sql_Conn" class.
Also, try using this for your connection.
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
string cmdText = "SELECT name FROM sys.tables"
cmd.CommandText = cmdText;
cmd.ExecuteNonQuery();
}
}

The same connection succeed and fails in different .net solutions

I have two .net solutions.
They both have this code:
var connectionString = "Server = ServerName; Database = DatabaseName; Trusted_Connection=True;"
var connection = new SqlConnection(connectionString);
connection.Open();
connection.Close();
And they both use complitely the same connectionString. And they connect to MSSQL Server;
And in the first solution connection.Open() succeeded and in the second one it failed.
Error message : Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
And every time I create new solution it fails. So this connection only works in one specific solution. And also, my teammates tested this and didn't have such issues.
Can you help with hints why it can be so?
Check that your SQL server has the TCP/IP protocol enabled and if it is using dynamic ports, ensure that the SQL Browser service is running.
Make sure that any firewall software (windows firewall etc.) has an exception added for the particular exe (the sqlservr.exe file gets copied into the instance folder, which will be in C:\Program Files\Microsoft SQL Server\{instance name}\MSSQL\Binn, where instance name might look something like MSSQL11.SQLEXPRESS) or TCP port 1433 or UDP port 1434 (TCP 1433 for the SQL service itself assuming standard settings, UPD 1434 for the SQL Browser Service).
Check that the user account used to access the server (it should also have sufficient permissions and the login should be enabled), in this case it will be whatever windows account you are logged in as or whatever account your IIS application pool is running as because you are using Trusted_Connection=True.
Also check that you have an up-to-date SQL Client on your machine, you can get the latest version for windows here.
Open Sql Server Configuration Manager and make sure that the Named Pipes option is enabled for your Sql server instance.
Sorry for disturbing you. My problem was my inattentiveness. I created soluiton on another network machine.

How can connect MySql Online Database to C# desktop application

I am creating desktop application in c# but i wont to use Online Database created in mysql.
String mysqlConnectionString = "Server=216.14.120.105;Database=xxx_keys;UID=xxx_root;Password=xxx;Port=3306";
MySqlConnection connection = new MySqlConnection(mysqlConnectionString);
connection.Open();
this is the code i am using but i got an error unable to access host.
where i am wrong and if you have any proper code or details then give me.
If this is a MySQL database in local server user "localhost" for server IP as georgi-it also told. But if this is an external database first try to open a telnet connection on dos-prompt to see if there is some firewall block or something like that:
telnet 216.14.120.105 3306
if you don't get an error message and see a blank page then it means you can reach mysql server

Categories

Resources