I have problem opening an MySql connection from an win console application. I am using SharpDevelop as IDE. Previously I've been working with MySql through C# aspx and am new to writing exe programs in C#, so I assume that I might be missing something in the IDE / project configuration.
I am successfully opening a connection and reading data from the table using adapter (window panel application binding to a grid), but when trying the code below in an console application the program crashes when calling the MyConn.Open(); function.
The error message being: Exception MySql.Data.MySqlClient.MySqlException was thrown in debuggee:
Unable to connect to any of the specified MySQL hosts.
Open()
If I break before the MyConn.Open(); function is called, the MyConn variable is filled with lot's of info looking ok to me, though I am not capable of knowing if any of it can tell me what leads to the crash in the next function call.
Ive added the MySql.Data.dll to the project from the GAC list in the add reference panel. Further, in addition to the references, I only have an AssemblyInfo.cs file and the Main.cs file with the code below in the project
I've reinstalled the MySql database trying different configurations etc, but whatever I have tried does not work.
Has anybody else encountered this problem and or know the solution?
Thanks in advance. The code follows below.
Expand|Select|Wrap|Line Numbers
- using MySql.Data.MySqlClient;
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Data;
- namespace VerPapConnCs
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- string strProvider = "Database=VerPap;Data Source=localhost;User ID=root;Password=some";
- MySqlConnection MyConn = new MySqlConnection(strProvider);
- MyConn.Open();
- string strSQL = "SELECT * FROM tab1;";
- MySqlCommand mysqlCmd = new MySqlCommand(strSQL,MyConn);
- MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();
- while (mysqlReader.Read())
- {
- Console.WriteLine(mysqlReader.GetInt32(0) + "\t" + mysqlReader.GetString(1));
- }
- MyConn.Close();
- }
- }
- }