C# cannot connect to remote MySQL
Asked By
0 points
N/A
Posted on - 10/01/2011
How do you allow the remote MySQL server to be opened via a simple C# application?
I'm having a connection problem.
Â
I'm using MySQL.net version 5 connector and Visual Studio 2005 express edition for this, but it is really strange working on the localhost but not on the remote computer.
Â
Below is my code:
Â
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome …!");
String conString = "SERVER = 192.168.2.1; DATABASE = l2emu; User ID = root; PASSWORD = password;";
MySqlConnection connection = new MySqlConnection(conString);
String command = "SELECT * FROM people";
MySqlCommand cmd = new MySqlCommand(command,connection);
MySqlDataReader reader;
try
{
connection.Open();
cmd.ExecuteNonQuery();
reader = cmd.ExecuteReader();
cmd.CommandType = System.Data.CommandType.Text;
while (reader.Read() != false)
{
Console.WriteLine(reader["name"]);
Console.WriteLine(reader["department"]);
}
Console.ReadLine();
}
catch (MySqlException MySqlError)
{
Console.WriteLine(MySqlError.Message);
}
}
}
}