How to connect to mysql with computer name in C# - c#

I want to connect to mysql with my computer name as host.
I get this error :
unable to connect to any of the specified mysql hosts
When i change the host with localhost or 127.0.0.1. it's work fine.
I test the connection directly in mysql workbench with my computer name : COMP-18TN. MySQL Workbench could connect to that server. But not my c# code.
You find as bellow my code :
string host = "COMP-18TN";
string DatabaseName = "Cars_DB";
string UserName ="root_name";
string Password ="pass";
string connString = string.Format("Server={0}; database={1}; UID={2}; password={3}",
host, DatabaseName, UserName, Password);
MySqlConnection conn = new MySqlConnection(connString);
try
{
conn.Open();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

You can try adding an entry in your hosts file located in C:\Windows\System32\drivers\etc\hosts:
127.0.0.1 COMP-18TN
This way your computer name will resolve to the local IP which you know works.

Related

ERROR: Unable to connect to any of the specified MySQL hosts - Synology NAS - MariaDB 10 - C#

I have a problem to connect my C# application in Visual Studio 2019 to Database inside Snyology NAS using MariaDB 10. If I connected the Database with HeidiSQL works good, but if I try to connect C# App with the same credentials I see an error like this:
"Unable to connect to any of the specified MySQL hosts"
I tried to check and I verified that:
TCP functionality is enable on Synology NAS
User and password are correct and I have all the privileges
Database name is correct
Port is also correct 3307 (setting default on Synology)
This is the code that I use to check the connection:
string connectionString = "";
string server = "ip_address_NAS:3307";
string database = "my_Database";
string username = "my_user";
string password = "my_password";
MySql.Data.MySqlClient.MySqlConnection cn;
connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + username + ";PASSWORD=" + password;
try
{
cn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
cn.Open();
label1.Text = "Database ONLINE!";
label1.ForeColor = System.Drawing.Color.Green;
cn.Close();
}
catch(Exception ex)
{
label1.Text = "Database OFFLINE!\n" + ex.Message;
label1.ForeColor = System.Drawing.Color.Red;
}
Why this error?
Can you help me? please.
Thank you.
Remove the port number from the hostname. If you are connecting to the default port, there's no need to specify a port. If you are connecting to a custom port, it has to be defined in the following way
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword
Docs
Edit
3307 is not the default port for MySQL or MariaDB. So if your database is running on port 3307 (because for whatever reason Synology has chosen it to be the initial setting) you need to specify that port in the connection string. But not as part of the servername but with the Port=3307 property.

Connection string for local database not working

I have a database on my local machine on SQL Server Management studio.
The database connection information is as follows.
In my c# application, I have a connection string to connect to this database. I get an error though saying my connection string is incorrect. I have tried a number of different ones.
This is my function to connect to a database.
public virtual void openConnection()
{
con = new SqlConnection();
con.ConnectionString = "Server=DESKTOP-8UDMQUI\\WILLIAMSQL;Initial Catalog=team3db;TrustServerCertificate=true;";
try
{
con.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I have two \ in the server name because when I only have one, it has a red squiggle line under it throwing me an error on visual studio.
What should my connection string be? Note there is no password on this database.
Replace TrustServerCertificate = true; with Integrated Security = true

unable to connect to any of the specified MySQL host cpanel

i get the error
Unable to connect to any of the specified MySQL hosts
string conn_string = "Server=jelletaal.nl;Port=3306;Database=jelletaa_thepuckcup;Uid=jelletaa_jelle;Pwd = nP_9+hC_!_2E;";
MySqlConnectionStringBuilder connstring = new MySqlConnectionStringBuilder();
try
{
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();
MessageBox.Show("succesfully created connection to database");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
in my cpannel I have created a database called jelletaa_thepuckcup
I have created a user called jelletaa_jelle
with the password nP_9+hC_!_2E
the domain is jelletaal.nl
i have also granted my ip adress acces to the database
if anyone knows what i have to do tho get this working, please tell me.

MySql Database on Raspberry PI 2 connection via C# Windows Forms Program

I have just recently started using my Raspberry Pi for a new project.
I have already set up the MySql database (connection with IPadress/phpmyadmin works)
Now I wanted to include the Database in my C# Project, but when I run it I get the cannot connect message from my try/catch. Where can the problem be?
I know this might sound silly, but I am actually stuck.
using MySql.Data.MySqlClient;
namespace MyRecipe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connetionString = null;
MySqlConnection cnn;
connetionString = "server='IP-RaspberryPI';database=recipe;uid=recipe;pwd=secret;";
cnn = new MySqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}
The first thing you need to do is evaluate your exception.
Change the catch statement to something like
MessageBox.Show("My exception says: " + ex.Message);
Did you configure mysql to listen for calls on Pi's IP address, or just localhost? Assuming that your Pi has a static IP address and not a dynamic one.
Try the string:
"server=192.168.x.x;uid=recipe;" + "pwd=secret;database=recipe"
Otherwise it must be the ports on your router open up port 2083 or alternatively it could be that your user has no permissions
Do this on your raspberry pi SSH
connect to mysql : mysql -u root -p
CREATE USER 'recipenew'#'localhost' IDENTIFIED BY 'secret';
GRANT ALL PRIVILEGES ON . TO 'recipenew'#'localhost'
WITH GRANT OPTION
I am almost certain that your user does not have the right permissions
and then change the string to:
"server=192.168.x.x;uid=recipe;pwd=secret;database=recipe"

Connecting with MySQL.NET connector

I'm getting error:
Could not establish a connection to database
when I connect with MySQL .NET connector. I'm fully sure of that the username is right, the password is right, the database is right, and the server is right. Here's the code:
mysqlCon.ConnectionString = "Server=(servername);Username=(username);Pwd=(rightpassword);
Database=(rightdatabase)";
try
{
mysqlCon.Open();
}
catch (Exception ex)
{
MessageBox.Show("Could not establish a connection to database!");
}
Shouldn't you use Uid instead of Username? At least connectionstrings.com says so :)
And also check what port is your MySQL running at. You might need to add port number to your connection string if you aren't using the default one - 3306.
I use:
public MySqlConnection NewConnection(string host, int port, string db, string user, string pwd)
{
string cstr = String.Format(
"SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}",
host, port, db, user, pwdd);
MySqlConnection conn = new MySqlConnection(cstr);
try { conn.Open(); }
catch (Exception ex)
{
conn = null;
}
return conn;
}
Make sure that the database is reachable from the machine you're running the code.
Use telnet
telnet <server> 3306
or MySQL command line client
mysql -u <user> -h <server> -P <port> -p

Categories

Resources