Link to home
Start Free TrialLog in
Avatar of derekthornton
derekthornton

asked on

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.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Avatar of gillgates
gillgates

yes you can

http://www.bytefx.com

This is what mysql suggests I believe.
Avatar of derekthornton

ASKER

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?
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();
Hrnm. Doesn't seem to work.

can you not use a remote server running MySQL?
Post the code you are using... I have used this before for a remote server and it worked fine.
                 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();
the datasource is just the path to my domain name, right?
Data Source is your server name or ip address.
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.
Post the code, let me look at it.
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?
Sorry, didn't even notice it was there.

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

Then you can try the ip and port...

192.168.1.1:3306
I don't understand what you mean.
            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();
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?
your server ip address.
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 ....
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.
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.
Okay. Server is back up. The code you gave me doesn't fix it.
Specifically what error are you getting?  Try and include the entire error.
ASKER CERTIFIED SOLUTION
Avatar of gillgates
gillgates

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It just says "Unable to retrieve any of the MySQL Data Sources"
http://sourceforge.net/projects/mysqldrivercs/

I used this at work last year.  It works, but I can't say I love its interface.
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;
                  }
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
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>
That's generated automatically by the compiler.
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.
Does ByteFX only work for MySQL 4? I'm on 3.2
No it should work for 3.2, I do know that it doesn't work on the .Net Framework 1.0 only 1.1
What about user permissions? What should those be in my SQL user permissions?
Woot!I got it!

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

Have fun.