Can't Connect mySQL database using LAN (C# Winform application) - c#

I'm building a winform application with C# lang and MySQL database. There's 3 computer to work with.
1st computer connected with LAN network (Static ip address), it creates hotspot with connect*fy app, and MySQL database server
2nd computer connect to 1st computer with WirelessLAN, connect to its connect*fy app
3rd computer connect to 1st computer with LAN cable (static IP address)
I can only connect 1st and 2nd computer. When I try with 3rd computer it showing message "Unable to connect any specified MySQL host"
Connection Strings that I used
string user = "koas";
string password = "poipoi";
string database = "bke_nota_db";
string host = "10.20.10.129";
MySqlConnection conn = new MySqlConnection("server=" + host +
";database=" + database +
";username=" + user +
";password=" + password +
";port = 3306" +
";");
Server's IP address that I used is 1st computer static IP adress that connect to LAN.
What should I do?

Related

How can I connect to mysql database on a remote host using c# winform app on my local pc?

I have already implement a control panel for MySQL database. That works perfect with local database which is installed on my PC. But when i try to connecting with mysql database on my web host i get this message:
cannot connect to any of the specified mysql hosts
Here is my connection string:
var csb = new MySqlConnectionStringBuilder
{
Server = "database.webhost.com",
Port= "3306"
UserID = "root",
Password ="1234567",
Database ="sampleDB",
SslMode= "None",
};
I was reading something about SSH secure connections to web hosts, but i can't find any examples.

Change Sql Server 2005 TCP/IP Ports in C#

I'm trying to change from c# the TCP/IP ports and dynamic ports of my instance in SQL Server 2005.
I have already tried a solution, as in the code above, but it works only if some Server 2008 functionality are installed (like: SharedManagementObject.msi).
I need a solution that work for Sql Server 2005 without the additional installation of other Sql Server editions.
Here is the code that i have already tried (
try
{
Console.WriteLine(" Started...\n");
const string instanceName = "INSTANCENAME";
var managedComputer = new ManagedComputer();
var serviceController = new ServiceController(string.Concat("MSSQL$", instanceName));
Console.WriteLine(" - Istance: " + instanceName + "\n - DisplayName: " + serviceController.DisplayName + "\n");
var serverInstance = managedComputer.ServerInstances[instanceName];
var serverProtocol = serverInstance.ServerProtocols["Tcp"];
var ipAddresses = serverProtocol.IPAddresses;
if (ipAddresses != null)
{
for (var i = 0; i < ipAddresses.Count; i++)
{
var ipAddress = ipAddresses[i];
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
if (!string.Equals(ipAddress.Name, "IPAll"))
{
ipAddress.IPAddressProperties["Enabled"].Value = true;
}
ipAddress.IPAddressProperties["TcpDynamicPorts"].Value = "";
ipAddress.IPAddressProperties["TcpPort"].Value = "1433";
serverProtocol.Alter();
}
}
if (serviceController.Status == ServiceControllerStatus.Running)
{
return;
}
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
Console.WriteLine(" Finished...\n");
}
catch (Exception exception)
{
Console.WriteLine("\n" + exception + "\n");
}
finally
{
Console.Write(" Press any key to continue... ");
Console.ReadKey();
}
Google is your friend...
If enabled, the default instance of the Microsoft SQL Server Database Engine listens on TCP port 1433. Named instances of the SQL Server Database Engine and SQL Server Mobile are configured for dynamic ports, which means they select an available port when the SQL Server service is started. When connecting to a named instance through a firewall, configure the Database Engine to listen on a specific port, so that the appropriate port can be opened in the firewall.
To assign a TCP/IP port number to the SQL Server Database Engine
In SQL Server Configuration Manager, in the console pane, expand SQL Server 2005 Network Configuration, expand Protocols for <instance name>, and then double-click TCP/IP.
In the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear, in the format IP1, IP2, up to IPAll. One of these are for the IP address of the loopback adapter, 127.0.0.1. Additional IP addresses appear for each IP Address on the computer. Right-click each address, and then click Properties to identify the IP address that you wish to configure.
If the TCP Dynamic Ports dialog box contains 0, indicating the Database Engine is listening on dynamic ports, delete the 0.
In the IPn Properties area box, in the TCP Port box, type the port number you wish this IP address to listen on, and then click OK.
In the console pane, click SQL Server 2005 Services.
In the details pane, right-click SQL Server (<instance name>) and then click restart, to stop and restart SQL Server.
After you have configured SQL Server to listen on a specific port there are three ways to connect to a specific port with a client application:
Run the SQL Server Browser service on the server to connect to the Database Engine instance by name.
Create an alias on the client, specifying the port number.
Program the client to connect using a custom connection string.
Now to do that in C#, I don't know. Maybe try a BAT file and call it from C#?

Cannot connect to my linux server MySQL DB

My MySQL connect code below is 'Catching' this error: "Unable to connect to any of the specified MySQL hosts."
My DB is on my linux website and I have remote access enabled using % so anyone can access the DB.
My code is .NET C# and I have a winform.
string server = "http://www.mywebite.net";
string database = "mywebsite_app";
string uid = "admin";
string password = "Password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection dbConn;
try
{
dbConn = new MySqlConnection(connectionString);
dbConn.Open();
if (dbConn.State.ToString() != "Open")
{
this.Text = "could not open database connection";
}
else
{
this.Text = "database connection opened";
}
}
catch (Exception ex) // catch on general exceptions, not specific
{
this.Text = ex.Message;
}
Thanks for any help...
If you mysql database is on remote hosting, then you usually out of luck try to connect to that database remotely.
The hosting provider usually blocked remote mysql access.
Try using ssh-tunneling.
download putty
http://www.chiark.greenend.org.uk/~sgtatham/putty/
open the command line, and execute putty from the installation directory
putty username#remote_mysqlhost -L 3306:localhost:3306
or you could use the gui, fill the address with your mysql remote, but dont open connection yet.
go to ssh tunnel, add new forwarded port 3306, and the destination as localhost:3306.
as of why the destination is localhost, it is because the destination is what the ssh session see. because you already connected to your remote server in your ssh session, thus, the mysql host is local.
this could also be a set up for dmz.
you can create a dmz gateway using putty to tunnel trough any computer within the dmz network. but the computer behind the gateway, wont publicly expose to the internet.
dont forget to klik the add button to make sure the configuration is added to tunnel list.
open the connection and use your password (ssh user and password, usually the same as webhosting control panel user and password
Now, instead connecting to your remote mysql, you can connect to mysql trough localhost. it would be tunneled by putty to your remote mysql.
try to connect using mysql administration tool first. to test wheter or not the connection is success.
the way ssh tunnel work is almost the same as vpn, from user view.
but without the hassle of setting up vpn server on remote host.

MySql C# - access with MyPhpAdmin but not with app

I'm developing an app with C# and I need to access MySql database in my university server. They provided me one login and password. With they, I can access the MySql via PhpMyAdmin, but when I try to connect with my app, the following error occurs:
"Unable to connect to any of the specified MySQL hosts."
I thought that could be the privileges of the user. But I'm not sure. So, what could be? With the possible solutions I can ask them to correct.
My connection code:
server = "server";
database = "database";
uid = "id";
password = "pass";
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
conn = new MySqlConnection(connectionString);
conn.Open();
Thanks a lot! I'm sorry for my bad English. =(
Mysql server's default port is 3306. You need to add a connectionstring in web.config and use them all over in your web-app.
As Meda written in her answer you need to use localhost or 127.0.0.1 it must be the ip of your server where you actually host your mysql database. remember that remote access must be enabled (on your university server) if you provide the IP to access the mysql database.
Tried these thing will surely fix your issue.
I use MySQL with my C# projects and I define the host value as:
localhost or 127.0.0.1
an IP address (xxx.xxx.xxx.xxx)
a host url (my.dbserver.com)
here is a working MySQL connection string:
<add name="connectionString"
connectionString="server=xx.xx.xx.xx;database=yyyyy;
User Id=zzzzzz;Password=**********;"
providerName="MySql.Data.MySqlClient" />

MYSQL connection to C# window application

I am going to make a smaill application with c# .net 2.0 here I have to connect the Mysql server i am trying many tutorial but fail
I am using
string constr = "Server=11.22.33.123;Port=3306;Database=bfcerin_ad;" +
"Uid=<user id here>;Pwd=<password here>;pooling=false;";
MySqlConnection con = new MySqlConnection(constr);
con.Open();
I have tried twice my user name and password is fine but getting access denied from Server
I am able to connect to the localhost mysql with this code but not remote mysql server
Login your cpanel and click on Remote MYSQL and then add Remote Database Access Hosts put % for giving access to all host.

Categories