472,093 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,093 software developers and data experts.

C# Console (exe) application crashes on open command (MySql)

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.

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
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Data;
  8. namespace VerPapConnCs
  9. {
  10.     class MainClass
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             string strProvider = "Database=VerPap;Data Source=localhost;User ID=root;Password=some";
  15.             MySqlConnection MyConn = new MySqlConnection(strProvider);
  16.             MyConn.Open();
  17.             string strSQL = "SELECT * FROM tab1;";
  18.             MySqlCommand mysqlCmd = new MySqlCommand(strSQL,MyConn);
  19.             MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();
  20.             while (mysqlReader.Read())
  21.             {
  22.                 Console.WriteLine(mysqlReader.GetInt32(0) + "\t" + mysqlReader.GetString(1));
  23.             }
  24.             MyConn.Close();
  25.         }        
  26.     }    
  27. }
Sep 9 '08 #1
1 4662
this post was old enough. but for the sake of new readers lyk me, ill try to place my opinion.

in strProvider string, i think you should explicitly specify the port, ex: port=3306
Dec 13 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Mark Allison | last post: by
11 posts views Thread by Peter Steele | last post: by
17 posts views Thread by MumboJumbo | last post: by
6 posts views Thread by vijai.kalyan | last post: by
5 posts views Thread by =?Utf-8?B?SmFtZXMgV29uZw==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.