C# and MySQL

Simple question. Can C# even use MySQL? If So, how? I've not been able to find many tutorials covering this, or even specifying if it can really be done.
LVL 2
derekthorntonAsked:
Who is Participating?
 
gillgatesCommented:
Try this
__________________________

using ByteFX.Data.MySqlClient;

--------------

string DataSource = "yourserver";
string Database ="public_beta_tracker"
string UserID = "php_admin";
string Password = "PASSWORD"
string CommandText;
MySqlConnection mysqlCon;
MySqlReader msqlReader;


strConnection  = "Data Source=" + DataSource + ";Database=" + Database + ";User ID=" + UserID + ";Password=" + Password;

msqlCon = new MySqlConnection(strConnection);
            
msqlCon.Open();

strCommandText = @"SELECT * FROM sometable";
                        
msqlCommand = new MySqlCommand(strCommandText, msqlCon);
msqlReader = msqlCommand.ExecuteReader();

while(msqlReader.Read())
{
    MessageBox.Show(msqlReader.GetString(0));
}
0
 
gillgatesCommented:
yes you can

http://www.bytefx.com

This is what mysql suggests I believe.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Fahad MukhtarDistinguished EngineerCommented:
0
 
derekthorntonAuthor Commented:
Yes. The ByteFX seems to be the best solution ...but I'm not entirely sure how it works. the Documentation is REALLY poor IMO as it gives absolutely no examples .... how does it just ..plain connect to a DB on a Remote Server?
0
 
gillgatesCommented:
Download an install
Add a refernces to bytefx.mysqlclient.dll.  In solution explorer right click on "References" and select "add"  From the list select "ByteFX.MySqlClient.dll"  Press Select and Ok.


using ByteFX.Data.MySqlClient;

-----------------

ByteFX.Data.MySqlClient.MySqlConnection con = new MySqlConnection("Datasource=YOURSERVER;Database=YOURDATABASE;User ID=YOURUSER;Password=PASSWORD;");

con.Open();
ByteFX.Data.MySqlClient.MySqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = "YOUR QUERY";
ByteFX.Data.MySqlClient.MySqlDataReader Reader = myCommand.ExecuteReader();

while(Reader.Read())
{
   MessageBox.Show(Reader.GetString(0));
}

con.Close();
0
 
derekthorntonAuthor Commented:
Hrnm. Doesn't seem to work.

can you not use a remote server running MySQL?
0
 
gillgatesCommented:
Post the code you are using... I have used this before for a remote server and it worked fine.
0
 
derekthorntonAuthor Commented:
                 ByteFX.Data.MySqlClient.MySqlConnection con = new ByteFX.Data.MySqlClient.MySqlConnection("Datasource=www.mydomainname.com;Database=beta_public_tracker;User ID=php_admin;Password=password;");

                  con.Open();
                  ByteFX.Data.MySqlClient.MySqlCommand myCommand = con.CreateCommand();
                  myCommand.CommandText = "YOUR QUERY";
                  ByteFX.Data.MySqlClient.MySqlDataReader Reader = myCommand.ExecuteReader();

                  while(Reader.Read())
                  {
                        MessageBox.Show(Reader.GetString(0));
                  }

                  con.Close();
0
 
derekthorntonAuthor Commented:
the datasource is just the path to my domain name, right?
0
 
gillgatesCommented:
Data Source is your server name or ip address.
0
 
derekthorntonAuthor Commented:
Yeah. That doesn't work. Am I putting in the strings wrong or something? do they need quotes or something?
Or the database name maybe?

I contacted my Hosting Provider and they said my domain name would be what I use, but this code just doesn't connect.
0
 
gillgatesCommented:
Post the code, let me look at it.
0
 
derekthorntonAuthor Commented:
I did. I can't post my password and stuff on a public board.
The server runs on Linux, is that going to stop it?
0
 
gillgatesCommented:
Sorry, didn't even notice it was there.

Do you know what port your database is using?
0
 
derekthorntonAuthor Commented:
What port does MySQL usually run on?
0
 
gillgatesCommented:
3306 i believe
0
 
gillgatesCommented:
Have you ever connected to the database from your computer before using the address you specified?
0
 
derekthorntonAuthor Commented:
Well, I haven't changed anything, so it must be running on that port. What does that do for me?
0
 
derekthorntonAuthor Commented:
Not through code, no. But through other applications I have.
0
 
gillgatesCommented:
Try putting the ip address in the connection string instead.

Then you can try the ip and port...

192.168.1.1:3306
0
 
derekthorntonAuthor Commented:
I don't understand what you mean.
0
 
gillgatesCommented:
            ByteFX.Data.MySqlClient.MySqlConnection con = new ByteFX.Data.MySqlClient.MySqlConnection("Datasource=192.168.1.1:3306;Database=beta_public_tracker;User ID=php_admin;Password=password;");

               con.Open();
               ByteFX.Data.MySqlClient.MySqlCommand myCommand = con.CreateCommand();
               myCommand.CommandText = "YOUR QUERY";
               ByteFX.Data.MySqlClient.MySqlDataReader Reader = myCommand.ExecuteReader();

               while(Reader.Read())
               {
                    MessageBox.Show(Reader.GetString(0));
               }

               con.Close();
0
 
derekthorntonAuthor Commented:
But 192.168.1.1 isn't the right address ..should I just substitute with my server IP? or is there a reason why you chose that IP?
0
 
gillgatesCommented:
your server ip address.
0
 
derekthorntonAuthor Commented:
Nope. That didn't work, but I think my hosting just went down. They've been horrible.
I'm using WebSiteSource. It's been awful since the day I signed up ....
0
 
gillgatesCommented:
Well, when it comes back up let me know... Once I get home I will be able to possibly help more because that is where all my mysql code is.
0
 
derekthorntonAuthor Commented:
Thanks.

I'm wanting to write a Chat Program that connects to a Jabber Server (which I have running on the server already) and stores some information (it has nothing to do with the jabber server, the jabber server is just the chat portion) in MySQL o nthe server and can use that info for some various stuff. So yeah ..hopefully I can make MySQL and C# talk.
0
 
derekthorntonAuthor Commented:
Okay. Server is back up. The code you gave me doesn't fix it.
0
 
gillgatesCommented:
Specifically what error are you getting?  Try and include the entire error.
0
 
derekthorntonAuthor Commented:
It just says "Unable to retrieve any of the MySQL Data Sources"
0
 
davidastleCommented:
http://sourceforge.net/projects/mysqldrivercs/

I used this at work last year.  It works, but I can't say I love its interface.
0
 
davidastleCommented:
Here is in example of how to connect with this API.
If you need more samples, install the MySQLDriverCS, and look in the source -> samples directory.

MySQLConnection conn =  new MySQLConnection( new MySQLConnectionString  ("localhost","mysql","root","").AsString );
                  MessageBox.Show("Connecting to database");
                  try
                  {
                        conn.Open();
                  }
                  catch(Exception ex)
                  {
                        MessageBox.Show(ex.Message);
                        return;
                  }
0
 
derekthorntonAuthor Commented:
An unhandled exception of type 'ByteFX.Data.MySqlClient.MySqlException' occurred in bytefx.mysqlclient.dll

Additional information: Unable to connect to any of the specified MySQL hosts

That's the error I get, gillgates
0
 
gillgatesCommented:
I found this at http://bugs.mysql.com/bug.php?id=3866
I don't know if it works but looks easy enough to try.

Try creating a file called appname.exe.config in the same folder as your .exe file....
So it will bin in projectfolder\bin\debug or projectfolder\bin\release

for "appname" put the name of your exe, so if it is called x.exe then the file should be x.exe.config

int the file put...

<?xml version ="1.0"?>
<configuration>
<startup>
   <supportedRuntime version="v1.1.4322" />
</startup>
</configuration>
0
 
derekthorntonAuthor Commented:
That's generated automatically by the compiler.
0
 
derekthorntonAuthor Commented:
Yeah, thanks for the tip, but it doesn't work.
That tip is only to force it to compile into .NET 1.1 if you have VS2002 installed, which I don't.

The same thing can be accomplished by setting the Runtime version in the properties of any project. It generates that file the same way.
0
 
derekthorntonAuthor Commented:
Does ByteFX only work for MySQL 4? I'm on 3.2
0
 
gillgatesCommented:
No it should work for 3.2, I do know that it doesn't work on the .Net Framework 1.0 only 1.1
0
 
derekthorntonAuthor Commented:
What about user permissions? What should those be in my SQL user permissions?
0
 
derekthorntonAuthor Commented:
Woot!I got it!

The problem was that my server hadn't opened the port for me on the firewall.
0
 
gillgatesCommented:
Thats great... I knew it had to be something like that...

Have fun.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.