Trying to connect to MySQL from C# application

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive created a simple C# windows form application in which Im trying to access a MySQL database that is on my local machine (same machine where the application is running). Im new to use MySQL so Im sure what Im doing wrong is something simple but I
cant figure it out. Here is my code

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using MySql.Data.MySqlClient;

<span style="color:Blue; namespace MySQLAccess
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; try
{

<span style="color:Blue; string connStr = <span style="color:#A31515; "SERVER=localhost;DATABASE=434506_visitmain;UID=root;PASSWORD=mypw";
MySqlConnection myConn = <span style="color:Blue; new MySqlConnection(connStr);
MySqlCommand myComm = myConn.CreateCommand();
MySqlDataReader Reader;
myComm.CommandText = <span style="color:#A31515; "SELECT name_col FROM colprof_col WHERE shortname_col=Quincy";
myConn.Open();
Reader = myComm.ExecuteReader();
<span style="color:Blue; while (Reader.Read())
{
MessageBox.Show(Reader.GetValue(0).ToString());
}
}
<span style="color:Blue; catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
[/code]


<br/>
The error I get is Unable to connect to any of the specified MySQL hosts.
I guess Im questioning my server name. Is localhost correct? I tried it with MySQL55 which is the MySQL SERVICE name as well and that didnt work. The UID of root is what shows up when I open the MySQL Workbench so I figured thats the User ID I should
be using. Is this correct? <br/>
<br/>
I opened up the TableEditor example and ran that with no luck. Im assuming it has to do with me not putting in the correct SERVER.
<br/>
<br/>
Any help/direction would be greatly appreciated.
Thanks
Bob

View the full article
 

Similar threads

Back
Top